Codeforces Round #648 (Div. 2).D Solve The Maze

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #648 (Div. 2).D Solve The Maze相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%9b%9b%e7%a7%8d' title='鍥涚'>鍥涚   span   ++   骞朵笖   閬嶅巻   round   include   鏃犳硶   bit   

http://codeforces.com/contest/1365/problem/D

棰樼洰澶ф剰

缁欎竴涓猲 m鍥撅紝鍥句笂鏈夊ソ浜?鍧忎汉 绌哄湴 澧?鍥涚鍏冪礌銆傚涓嶅彲閫氳繃銆備綘鍙互浠绘剰璁剧疆澧欙紝闂湁娌℃湁涓€绉嶈缃柟娉曚娇寰楀ソ浜洪兘鍙互鍒皀 m鐐?鍧忎汉閮戒笉琛屻€?/strong>

鍋氭硶

 閬嶅巻鏁翠釜鍥撅紝鎵惧埌鎵€鏈夌殑G鐐癸紝浠庢瘡涓€涓ソ浜虹偣鍑哄彂锛岀湅鑳藉惁鍒拌揪缁堢偣锛岃矾寰勪笂濡傛灉鏈夌浉閭荤殑“B”鐐癸紝鍒欑‘瀹氫负姝よ矾涓嶉€氾紝鍚﹀垯鏃犳硶鍫典綇B銆傝繖鏍烽兘閬嶅巻瀹屼簡浠ュ悗濡傛灉瀛樺湪G鐐规棤娉曞埌杈剧粓鐐瑰氨no鍚﹀垯yes銆?涓棿闇€瑕佷竴涓皬浼樺寲銆傚鏋滀竴涓狦鐐归檮杩戞湁G鐐瑰苟涓旀病鏈塀鐐癸紝鍒欐妸杩欎釜鐐瑰彉鎴?鈥?. 鈥?nbsp;  杩欐牱灏辩渷鍘讳簡涓€涓猟fs銆備笉杩欐牱鍋氫細瓒呮椂銆?/strong>

浠g爜

#include<bits/stdc++.h>
using namespace std;
char a[55][55];
int vis[55][55];
int n,m;
int dfs(int y,int x){
    if(a[y+1][x]==鈥?/span>B鈥?/span>||a[y-1][x]==鈥?/span>B鈥?/span>||a[y][x+1]==鈥?/span>B鈥?/span>||a[y][x-1]==鈥?/span>B鈥?/span>){
        return 0;
    }
    if(y==n&&x==m){
        return 1;
    }
    if(a[y+1][x]==鈥?/span>.鈥?/span>||a[y+1][x]==鈥?/span>G鈥?/span>){
        if(!vis[y+1][x]){
            vis[y+1][x]=1;
            if(dfs(y+1,x)){
                vis[y+1][x]=0;
                return 1;
            }
        }
    }
    if(a[y-1][x]==鈥?/span>.鈥?/span>||a[y-1][x]==鈥?/span>G鈥?/span>){
        if(!vis[y-1][x]){
            vis[y-1][x]=1;
            if(dfs(y-1,x)){
                vis[y-1][x]=0;
                return 1;
            }
        }
    }
    if(a[y][x+1]==鈥?/span>.鈥?/span>||a[y][x+1]==鈥?/span>G鈥?/span>){
        if(!vis[y][x+1]){
            vis[y][x+1]=1;
            if(dfs(y,x+1)){
                vis[y][x+1]=0;
                return 1;
            }
        }
    }
    if(a[y][x-1]==鈥?/span>.鈥?/span>||a[y][x-1]==鈥?/span>G鈥?/span>){
        if(!vis[y][x-1]){
            vis[y][x-1]=1;
            if(dfs(y,x-1)){
                vis[y][x-1]=0;
                return 1;
            }
        }
    }
    return 0;
}
void solve(){
    memset(a,0,sizeof(a));
    cin>>n>>m;
    for(int i=1;i<=n;i++){
    scanf("%s",a[i]+1);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(a[i][j]==鈥?/span>G鈥?/span>){
                if(a[i+1][j]==鈥?/span>G鈥?/span>||a[i-1][j]==鈥?/span>G鈥?/span>||a[i][j+1]==鈥?/span>G鈥?/span>||a[i][j-1]==鈥?/span>G鈥?/span>){
                    if(a[i+1][j]!=鈥?/span>B鈥?/span>&&a[i-1][j]!=鈥?/span>B鈥?/span>&&a[i][j+1]!=鈥?/span>B鈥?/span>&&a[i][j-1]!=鈥?/span>B鈥?/span>){
                    a[i][j]=鈥?/span>.鈥?/span>;
                    continue;
                }
                }
                memset(vis,0,sizeof(vis));
                if(!dfs(i,j))
                {
                    cout<<"No"<<endl;
                    return ;
                }
            } 
        }
    }
    cout<<"Yes"<<endl;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}

 

以上是关于Codeforces Round #648 (Div. 2).D Solve The Maze的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #648 (Div. 2) A-F题解

Codeforces Round #648 (Div. 2)ABCDEF(题解)

Codeforces Round #648 (Div. 2)ABCDEF(题解)

Codeforces Round #648 (Div. 2) (A-F)

Codeforces Round #648 (Div. 2).D Solve The Maze

Codeforces Round #648 (Div. 2) A - Matrix Game