HDU-OJ 杭电1010

#include 
#include 
#include 
#include  
using namespace std;

char a[9][9];
int table[51][51];
int visited[50];
int T;
int final = 0;
int n, m;
int minPath; 
int xStart, yStart;
int xEnd, yEnd;

void dfs(int s,int degree){
    degree++;

    if(visited[s] == 1) 
        return ;
    if(final == 1) return ;

    visited[s] = 1;
    if(degree == T && a[(s-1)/m+1][(s-1)%m+1]=='D') final = 1;

    if(T - degree < abs(xEnd - ((s-1)/m+1)) + abs(yEnd - ((s-1)%m+1))){
//        cout<
        visited[s] = 0; 
        return ;
    }
//    cout<<"X="<//查4次 
    {
        int i=s-1;
        int v=table[s][i];
        if(v==1) 
            dfs(i,degree);

    }
    {
        int i=s+1;
        int v=table[s][i];
        if(v==1) 
            dfs(i,degree);

    }
    {
        int i=s-m;
        int v=table[s][i];
        if(v==1) 
            dfs(i,degree);

    }
    {
        int i=s+m;
        int v=table[s][i];
        if(v==1) 
            dfs(i,degree);

    }

    visited[s] = 0; 
}

int main(){
     int Start;
     int end;
     int count;

     //initialize


    while(scanf("%d%d%d",&n,&m,&T) != EOF && n != 0 && m != 0 && T != 0){
        //初始化 
        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                a[i][j] = 'X';
        memset(table,0,sizeof(table));
        memset(visited,0,sizeof(visited));    
        final = 0;
        count = 0; 

        for(int i = 1; i <= n; i++){
            getchar();
            for(int j = 1; j <= m; j++){
                scanf("%c",&a[i][j]);
            }
        }    
        getchar();
        //test 
//        for(int i = 1; i <= n; i++){
//            for(int j = 1; j <= m; j++){
//                cout<
//            }
//            cout <
//        }
        //
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(a[i][j] == 'S'){
                    Start = (i-1)*m + j;
                    xStart = i;
                    yStart = j;
                }
                if(a[i][j] == 'D'){
                    end = (i-1)*m + j;
                    xEnd = i;
                    yEnd = j;
                }
            }
        }


        //Create a adjacency list 
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                count++;
                if(a[i][j] == 'X') continue; 
                if(a[i][j-1]=='.'||a[i][j-1]=='D') {
                    table[count][count-1] = 1;                 
                }
                if(a[i][j+1]=='.'||a[i][j+1]=='D') {
                    table[count][count+1] = 1;
                }
                if(a[i-1][j]=='.'||a[i-1][j]=='D') {
                    table[count][count-m] = 1;
                }
                if(a[i+1][j]=='.'||a[i+1][j]=='D') {
                    table[count][count+m] = 1;
                }

            }
        }

//        for(int i = 1; i <= n*m; i++){
//            for(int j = 1; j <= m*n; j++){
//                cout<
//            }
//            cout<
//        }
        if(T < abs(xStart - xEnd) + abs(yStart - yEnd))    cout<<"NO\n";      
        else if((T - abs(xStart - xEnd) - abs(yStart - yEnd)) & 1){  
            cout<<"NO\n";  
        }  
        else{
            dfs(Start,-1);
            if(final == 1) cout <<"YES"<else cout <<"NO"<return 0;
}

这题风格太差 没必要邻接矩阵!很容易超时!!

下面介绍一下,我校ACM大神的算法

#include "iostream"
#include "cstring"
using namespace std;
const int maxn = 13;
char a[maxn][maxn];
int vis[maxn][maxn];

int T, n, m, final, sx, sy, ex, ey;
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
void dfs(int x, int y, int step){
    if (step > T) return;
    if (a[x][y] == 'D' && step == T) final = 1;
    if (final) return;
    int left = T - step, les = abs(x - ex) + abs(y - ey);
    if (left < les) return;
    if (left % 2 != les % 2) return;

    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
        if (a[nx][ny] == 'X') continue;
        if (vis[nx][ny]) continue;
        vis[nx][ny] = 1;
        dfs(nx, ny, step + 1);
        vis[nx][ny] = 0;
    }
}

int main(){
#ifdef AC
    freopen("data.txt", "r", stdin);
#endif
    while(scanf("%d%d%d", &n, &m, &T) != EOF && n){
        final = 0;
        for(int i = 0; i < n; i++) scanf("%s", a[i]);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                if (a[i][j] == 'S') sx = i, sy = j, vis[i][j] = 1;
                else if (a[i][j] == 'D') ex = i, ey = j, vis[i][j] = 0;
                else if (a[i][j] == 'X') vis[i][j] = 1;
                else vis[i][j] = 0;
            }
        dfs(sx, sy, 0);
        if(final) puts("YES");
        else puts("NO");

    }
    return 0;
}

你可能感兴趣的:(DFS)