hdu1010 (dfs and parity pruning)

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=1010
subject to the effect:
is to see there is no specified maze memory is a way to specify the number of steps of the path from start to finish.

Topic ideas:

Dfs is the use of a search
and then use the parity pruning to reduce the search time
simply under parity pruning:
give us a map, such as:
SX
. ... the X-
... XD
...
we can be seen as:
0101
1010
0101
1010
starting point where 0 , where the end point is 1, then they need to 01 the number of steps odd bits
and if 00 or 11 is needed even. For example child can go and see.
So with ** ((predetermined time of arrival) - shortest path)% 2 == 1 is intended path ** undesirable problems, it is subtracted pruning.
Then there is a kind of your shortest path> specified time is not desirable.

I started to accept when the map data is as follows:

 for(int i = 1;i <= row;i++){//初始化graph数组 
        getchar();
        for(int j = 1;j <= column;j++){
            scanf("%c",&graph[i][j]);
            if(graph[i][j] == 'X'){
                used[i][j] = 1;//1为不可通过 
            }
            else if(graph[i][j] == 'S'){
                start_x = i;
                start_y = j;
            }
            else if(graph[i][j] == 'D'){
                end_x = i;
                end_y = j;
            }
        }
    }

Focus is not on the other is that getchar (); and scanf ( "% c", & graph [i] [j]); put me to beat up I always thought that acceptance is right, because yesterday doing a dp so acceptance is right. Then I checked, all kinds of error, I spit, examined more than two hours.
Hey, sure enough it is still a number of dishes.
The final code into cin >> graph [i] [j ]; it is a success! ! ! ! !
Hey ~ ~ ~

ac Code

#include <stdio.h> 
#include <string.h>
#include <iostream>
using namespace std;
char graph[10][10];
int used[10][10];

int dx[4] = {0,1,0,-1};//左下右上
int dy[4] = {-1,0,1,0};
int row,column;//接受地图的行和列 
int T;
int flag;
int start_x,start_y;
int end_x,end_y;

int abs(int a){
    return (a>0)?a:-a;
}


void dfs(int x,int y,int time){
    int new_x,new_y;
    int temp; 
//    printf("graph[%d][%d]:%c time=%d T=%d\n",x,y,graph[x][y],time,T);
    if(graph[x][y] == 'D'){
    	if(time == T)
        	flag = 1;
        return;
    }
    
    temp = T - time - abs(end_x-x) - abs(end_y-y);
    if(temp < 0 || temp == 1){//奇偶剪枝 
						//如果在gg数组中,0与0或1与1的值需要偶数位的T
                        //0与1则需要奇数位的T 
                        //如果不是这样对应则证明这样就不能在第T秒达到终点 
        return;
    }
    
    
    if(time > T){
        return;
    }
    
    for(int i = 0;i < 4;i++){
        new_x = x + dx[i];
        new_y = y + dy[i];
        
        if(new_x < 1 || new_x > row 
        || new_y < 1 || new_y > column 
        || used[new_x][new_y] == 1){
            continue;
        }
        
        used[new_x][new_y] = 1;

        
        ++time;
//        printf("new_x:%d new_y:%d time:%d\n",new_x,new_y,time);
        dfs(new_x,new_y,time);
//        printf("回溯的:new_x:%d new_y:%d time:%d\n",new_x,new_y,time);

        --time;
        used[new_x][new_y] = 0;
        if(flag == 1){
            return;
        }
        
    }
    
}


int main(){
    while(scanf("%d%d%d",&row,&column,&T) != EOF){
    

    if(row == 0 && column == 0 && T == 0){
        break;
    }
    memset(used,0,sizeof(used));
    
    
    for(int i = 1;i <= row;i++){//初始化graph数组 
        for(int j = 1;j <= column;j++){
            cin>>graph[i][j];
            if(graph[i][j] == 'X'){
                used[i][j] = 1;//1为不可通过 
            }
            else if(graph[i][j] == 'S'){
                start_x = i;
                start_y = j;
            }
            else if(graph[i][j] == 'D'){
                end_x = i;
                end_y = j;
            }
            
        }
    }

        
    flag = 0;
        
    used[start_x][start_y] = 1;
    dfs(start_x,start_y,0);
        
    if(flag == 1){
        printf("YES\n");
    }else{
        printf("NO\n");
    }
        
    }

    
    
    return 0;

}
发布了40 篇原创文章 · 获赞 0 · 访问量 420

Guess you like

Origin blog.csdn.net/zhoupingqi2017/article/details/104270548