HDU 2102 plan A (BFS)

Source: Internet
Author: User
Plan

Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 7258 accepted submission (s): 1710


Problem description after the poor princess was captured by the Lord and rescued by the Knights again and again, unfortunately, she once again faces the test of her life. The devil has sent out a message saying that he will eat the princess at T moment, because he heard the rumor that the meat of the princess can survive forever. The elderly King was anxious to ask the warriors to save the princess. However, the princess has been used to it. She is confident that the brave knight LJ will be able to rescue her.
According to the investigation, the princess is locked in a two-layer maze. the entrance of the maze is S (, 0). The position of the princess is represented by P, and the space-time transmission is represented, the wall is represented by *, and flat is used.. As soon as the Knights enter the time-space transmitter, they will be transferred to the relative position of the other layer. However, if they are switched to the wall, the knights will be killed. In the first layer, the knights can only move between the left and right sides. Each movement takes 1 moment. Inter-layer movement can only pass through the time-space transmitter, and does not require any time.


The first line of input C indicates a total of C test data. The first line of each test data has three integers, n, m, and T. N, m the size of the Maze N * m (1 <= n, m <= 10 ). T. The next N * m represents the layout of the first layer of the maze, and the next N * m represents the layout of the second layer of the maze.


Output if the knights can find the princess at T moment, they will output "yes"; otherwise, "no ".


Sample Input

15 5 14S*#*..#........****....#...*.P#.*..***.....*.*.#..
 


Sample output

YES
 


Sourcehdu 2007-6 Programming Contest


Recommendxhd should not think too much about getting this question first. Since it is the shortest step, we can generally think of BFS, there is no big difference between the BFS of this question and the BFS of other questions. But pay attention to the initialization and several details. If the two layers are at the same time #, it will all become *, in this way, the program is prevented from getting stuck in an endless loop. I guess such test data will not be converted into a wall if the two layers have one side as a barrier and the other side as a barrier, the reason is that rec_up and rec_down cannot be used to record whether the current search location has been searched. In fact, I am like this, these two arrays are used to record the minimum step after this point. If you encounter this point again next time, you can determine whether the step at this point is shorter than that at the lowest point in history, otherwise, we will skip this point. if it meets the requirements, we need to re-search for this point and pay attention to several small pruning methods. This is not much to say. We just determined that it was a small pruning, and there is still a time not greater than count. Because of the many situations, the Code seems to be a little long, but it doesn't matter. There's no technical content! The data volume of this question is relatively small, and the results are generally correct.

#include <iostream>#include <stdio.h>#include <string.h>#include <queue>#define count COUNTusing namespace std;struct point{int i,j;int num;bool up_down;};int n,m,count;char map_up[15][15];char map_down[15][15];int rec_up[15][15];int rec_down[15][15];int init(){int i,j;for(i=0;i<=m+1;i++){map_up[0][i]='*',map_down[0][i]='*';map_up[n+1][i]='*',map_down[n+1][i]='*';}for(i=0;i<=n+1;i++){map_up[i][0]='*',map_down[i][0]='*';map_up[i][m+1]='*',map_down[i][m+1]='*';}for(i=1;i<=n;i++)for(j=1;j<=m;j++){rec_up[i][j]=rec_down[i][j]=999999;if(map_up[i][j]=='#' && (map_down[i][j]=='#' || map_down[i][j]=='*'))map_up[i][j]=map_down[i][j]='*';if(map_down[i][j]=='#' && (map_up[i][j]=='#' || map_up[i][j]=='*'))map_up[i][j]=map_down[i][j]='*';}return 0;}int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};bool flag=true;int BFS(){queue<point> q;point temp;point p;flag=true;int i;//memset(rec_up,0,sizeof(rec_up));//memset(rec_down,0,sizeof(rec_down));temp.i=1,temp.j=1,temp.num=0;if(map_up[1][1]=='#')temp.up_down=false;elsetemp.up_down=true;q.push(temp);while(!q.empty()){temp=q.front();q.pop();if(flag==false)return 0;if(temp.up_down){if(map_up[temp.i][temp.j]=='P'){flag=false;printf("YES\n");return 0;}}else{if(map_down[temp.i][temp.j]=='P'){flag=false;printf("YES\n");return 0;}}if(temp.num >= count)return 0;for(i=0;i<4;i++){p=temp;if(p.up_down){if(map_up[p.i+dir[i][0]][p.j+dir[i][1]]=='.' && rec_up[p.i+dir[i][0]][p.j+dir[i][1]] > p.num+1){rec_up[p.i+dir[i][0]][p.j+dir[i][1]]=p.num+1;p.num++;p.i=p.i+dir[i][0],p.j=p.j+dir[i][1];q.push(p);}else if(map_up[p.i+dir[i][0]][p.j+dir[i][1]]=='#' && rec_down[p.i+dir[i][0]][p.j+dir[i][1]] > p.num+1){p.num++;p.up_down=false;rec_down[p.i+dir[i][0]][p.j+dir[i][1]]=p.num;p.i=p.i+dir[i][0],p.j=p.j+dir[i][1];q.push(p);}else if((map_up[p.i+dir[i][0]][p.j+dir[i][1]]=='P')){printf("YES\n");flag=false;return 0;}}else{if(map_down[p.i+dir[i][0]][p.j+dir[i][1]]=='.' && rec_down[p.i+dir[i][0]][p.j+dir[i][1]] > p.num+1){rec_down[p.i+dir[i][0]][p.j+dir[i][1]]=p.num+1;p.num++;p.i=p.i+dir[i][0],p.j=p.j+dir[i][1];q.push(p);}else if(map_down[p.i+dir[i][0]][p.j+dir[i][1]]=='#' && rec_up[p.i+dir[i][0]][p.j+dir[i][1]] > p.num+1){p.num++;p.up_down=true;rec_up[p.i+dir[i][0]][p.j+dir[i][1]]=p.num;p.i=p.i+dir[i][0],p.j=p.j+dir[i][1];q.push(p);}else if((map_down[p.i+dir[i][0]][p.j+dir[i][1]]=='P')){printf("YES\n");flag=false;}}}}return 0;}int main(){int t;int i;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&count);for(i=1;i<=n;i++)scanf("%s",map_up[i]+1);for(i=1;i<=n;i++)scanf("%s",map_down[i]+1);init();BFS();if(flag)printf("NO\n");}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.