现在的位置: 首页 > 综合 > 正文

hdu1010 Tempter of the Bone(剪枝)

2018年12月20日 ⁄ 综合 ⁄ 共 3794字 ⁄ 字号 评论关闭

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 51440    Accepted Submission(s): 13834


Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the
T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for
more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

 


Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the
maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter; 
'S': the start point of the doggie; 
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.

 


Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 


Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
 


Sample Output
NO YES
 
code1:(奇偶剪枝)
/*奇偶性剪枝:
可以把map看成这样: 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
从为 0 的格子走一步,必然走向为 1 的格子 
从为 1 的格子走一步,必然走向为 0 的格子 
即: 
 0 ->1或1->0 必然是奇数步 
 0->0 走1->1 必然是偶数步*/

#include <stdio.h>
#include <math.h>
#include <string.h>
char map[10][10];
int dis[10][10];
int n, m, s;
int sx, sy, ex, ey;
int flag;
int dx[]= {0,1,-1,0};
int dy[]= {1,0,0,-1};
void dfs(int x, int y, int k) {
    int i, xx , yy;
    if(flag) return;
    if(k>s) return;
    if(k==s)
    {
       if(x==ex && y==ey) flag = 1;
       return;
    }
    int t = s-k-dis[x][y];
    if( t<0 | t&1 ) return;
    for(i=0; i<4; ++i) {
        xx= x + dx[i];
        yy= y + dy[i];
        if(xx<0 || xx >= n || yy <0 || yy >=m || map[xx][yy] =='X') continue;
        if(map[xx][yy] != 'X') {
            map[xx][yy] ='X';
            dfs(xx, yy, k+1);
            map[xx][yy] = '.';
        }
    }
}
int main() {
    int i, j, count;
    int ch;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&n,&m,&s)&&(n+m+s)) {
        flag = 0;
        count = 0;
        for(i=0; i<n; i++) scanf("%s",map[i]);
        for(i=0; i<n; ++i)
            for(j=0; j<m; ++j) {
                if(map[i][j] == 'S') {
                    sx = i;
                    sy = j;
                    map[i][j]='X';
                }
                if(map[i][j] == 'D') {
                    ex = i;
                    ey = j;
                }
                if(map[i][j]=='.')  count++;
            }
    
        if(count+1< s) {printf("NO\n");continue;}

        for(i=0; i<n; ++i)
            for(j=0; j<m; ++j)
                dis[i][j] = abs(ex-i) + abs(ey-j);
        dfs(sx, sy, 0);
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

code2: (宽搜, 每个状态下都保存一个棋盘)/*在HDU上交挂了*/ 

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,s;
char ma[10][10];
struct node{
    int x,y,step;
    char ma[10][10];
}s_pos;
int d_x,d_y;
int flag;
bool vis[10][10][10][10][50];

int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};

void bfs(){
    memset(vis,false,sizeof(vis));
    vis[s_pos.x][s_pos.y][s_pos.x][s_pos.y][s_pos.step]=true;
    queue<node > q;
    q.push(s_pos);
    while(!q.empty()){
        node now = q.front(); q.pop();
         if(now.x==d_x&&now.y==d_y){
                if(now.step==s){
                    flag=1;
                    return ;
            }else continue;
        }

        for(int i=0;i<4;i++){
            node next=now;
            next.x+=dx[i];  next.y+=dy[i];


            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m){
                next.step++;
                if(next.ma[ next.x ][next.y]=='X'||next.step>s) continue;
                next.ma[ now.x ][now.y]='X';
              //  if(next.ma[next.x][next.y]=='.'||next.ma[next.x][next.y]=='D'){
                if(!vis[now.x][now.y][next.x][next.y][next.step]){
                    vis[now.x][now.y][next.x][next.y][next.step]=true;
                    if(next.x==d_x&&next.y==d_y){
                         if(next.step==s){
                                   flag=1;
                                   return ;
                         }else continue;
                    }else
                    q.push(next);
                }
           // }
            }
        }
    }
}
int main(){
    while(scanf("%d %d %d",&n,&m,&s)!=EOF,n+m+s){
        for(int i=0;i<n;i++) scanf("%s",ma[i]);

        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
               if(ma[i][j]=='S'){
                   s_pos.x=i;
                   s_pos.y=j;
                   s_pos.step=0;
               }
               if(ma[i][j]=='D'){
                   d_x=i;
                   d_y=j;
               }
               s_pos.ma[i][j]=ma[i][j];
            }
        }
        flag=0;
        bfs();
        if(flag) printf("YES\n");
        else     printf("NO\n");
    }
    return 0;
}


抱歉!评论已关闭.