POJ 1010
来源:互联网 发布:免费的小说软件 编辑:程序博客网 时间:2024/06/10 13:19
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.
Note:
This question belongs to dfs family.Something we should notice is the steps mentioned in the question.And I learn a mean of cutting-edges is to calculate the steps left and x-axis and y- axis length form current point to final point,if they are both even or odd ,the path is available.If you want to testify it ,you can simply draw a map and try.
and I find a good function model for dfs
void DFS( argument ){ // recursion boundary // check if the argument has reached the ans // some action // keep recursion}
#include <bits/stdc++.h>using namespace std;int dir[4][2]={ {0,1} , {1,0} , {-1,0} , {0,-1} };char m[10][10];int vis[10][10];int N,M,T,tx,ty;int flag=0;bool check(int a,int b){ if(a<0||a>=N||b<0||b>=M) { return false; } else { return true; }}void dfs(int x,int y,int d){ if(check(x,y)==false) return; vis[x][y]=1; if(x==tx && y==ty && d==T) { flag=1; return;} if(((T-d)%2)!=((abs(x-tx)+abs(y-ty))%2)) return; for(int i=0;i<4;i++) { if(!vis[x+dir[i][1]][y+dir[i][0]]&&!flag) { dfs(x+dir[i][1],y+dir[i][0],d+1); vis[x+dir[i][1]][y+dir[i][0]]=0; } } }int main(){ while(cin>>N>>M>>T) { memset(m,0,sizeof(m)); memset(vis,0,sizeof(vis)); int sx,sy; if(N==0&&M==0&&T==0) { break; } else { for(int i=0;i<N;i++) { scanf("%s",m[i]); } for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { char t=m[i][j]; if(t=='X'||t=='x') { vis[i][j]=1; } else { if(t=='S'||t=='s') { sx=i; sy=j; } else if(t=='D'||t=='d') { tx=i; ty=j; } } } } } flag=0; dfs(sx,sy,0); if(flag==1) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } } return 0;}
- POJ 1010
- Poj 1010
- POJ 1010 邮票
- poj 1010 邮票问题
- poj 1010邮票
- POJ 1010 STAMPS
- poj 1010 STAMPS
- poj 1010 STAMPS
- POJ-1010-STAMPS
- [POJ][1010]STAMPS
- POJ 1010 STAMPS
- POJ - 1010 STAMPS
- POJ 1010: STAMPS
- poj-1010(转)
- POJ 1010 STAMPS(DFS)
- poj 1010 dfs
- POJ 1010 STAMPS
- POJ 1010--STAMPS
- java编程思想学习_第11章_持有对象
- Java NIO 教程
- 39-1-1
- java多线程学习总结(一)
- 1 SVN本地搭建
- POJ 1010
- 我对 CQRS / EventSourcing 架构的思考
- HDU 1540 Tunnel Warfare
- 分布式应用解耦利器rabbitmq
- Leetcode 134. Gas Station
- EmitMapper 和TinyMapper 两者简单对比(若转载请注明)
- JavaEE Filter和Listener
- 算法导论第15章习题15.1-4
- Oracle 数据库备份与还原