蒟蒻求助!!!
查看原帖
蒟蒻求助!!!
194378
Angel_s_Shadow楼主2020/10/29 19:12

这个鬼畜的dfs为什么最后几个wa呢?

#include <iostream>
#include <cstring>
using namespace std;
int n,m;
int maps[1010][1010];
int sR,sC;
int visited[1010][1010][3];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
bool found=false;
int change(int a,int v){
    return (a-1+v)%v+1;
}
void dfs(int r,int c,int realR,int realC){
    if(found){
        return;
    }
    if(visited[r][c][2]&&(visited[r][c][0]!=realR||visited[r][c][1]!=realC)){
        found = true;
        //cout<<"FOUND:"<<r<<" "<<c<<" "<<realR<<" "<<realC<<endl;
        return;
    }else if(visited[r][c][2]){
        return;
    }
    if(maps[r][c]==0){
        return;
    }
    //cout<<r<<" "<<c<<" "<<realR<<" "<<realC<<endl;
    visited[r][c][0]=realR;
    visited[r][c][1]=realC;
    visited[r][c][2] = 1;
    for(int i = 0;i<4;i++){
        dfs(change(r+dx[i],n),change(c+dy[i],m),realR+dx[i],realC+dy[i]);
    }
    return;
}
string tmpStr;
int main()
{
    while(cin>>n>>m){
        if(!n||!m){
            break;
        }
        memset(visited,0,sizeof(visited));
        memset(maps,0,sizeof(maps));
        found = false;
        for(int i = 1;i<=n;i++){
            cin>>tmpStr;
            tmpStr=" "+tmpStr;
            for(int j =1;j<=m;j++){
                if(tmpStr[j]=='.'){
                    maps[i][j]=1;
                }
                if(tmpStr[j]=='S'){
                    maps[i][j] = 1;
                    sR = i;sC = j;
                }
            }
        }
        dfs(sR,sC,sR,sC);
        cout<<(found?"Yes":"No")<<endl;
    }
    return 0;
}

2020/10/29 19:12
加载中...