uestc oj 1269 Children of the Candy Corn
来源:互联网 发布:原版优化9拜占庭 编辑:程序博客网 时间:2024/05/17 05:13
Children of the Candy Corn
这个题目的纠结之处在于题意的理解还有就是代码的书写能力上。
就是两个深度搜索,一个广度搜索
深度搜索一个是沿着左手侧的一个是沿着右手侧的 这两个处理起来较为麻烦,广搜简单。
处理的时候遵循方向原则 沿着左手走的时候,先向左走,不行的话就向前走,然后向右走 再不行就往回走
沿着右手走的时候,先向右走,不行的话就向前走,然后向左走,再不行就往回走
这里的方向都是相对的,比如向左走的的时候此时向”左“走就是向下了 同理可得其他方向 挨个遍历就行了
最后就可得出结论 达到终点得出步数
这个题目的纠结之处在于题意的理解还有就是代码的书写能力上。
就是两个深度搜索,一个广度搜索
深度搜索一个是沿着左手侧的一个是沿着右手侧的 这两个处理起来较为麻烦,广搜简单。
处理的时候遵循方向原则 沿着左手走的时候,先向左走,不行的话就向前走,然后向右走 再不行就往回走
沿着右手走的时候,先向右走,不行的话就向前走,然后向左走,再不行就往回走
这里的方向都是相对的,比如向左走的的时候此时向”左“走就是向下了 同理可得其他方向 挨个遍历就行了
最后就可得出结论 达到终点得出步数
#include<cstdio>#include<cstring>int T,w,h,startx,starty,endx,endy,head,tail,temp;char map[42][42];int vis[42][42];int dir[4][2]={1, 0, -1,0, 0, 1, 0, -1};struct node{ int x; int y; int step;}que[42*42];void input(){ for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='S') { startx = i; starty = j; } if(map[i][j]=='E') { endx = i; endy = j; } } getchar(); }}int dfs_left(int x ,int y,int step,int dir){ if(map[x][y]=='E') { return step; } if(dir == 1) { //printf("%d 到了方向上\n",step); if(y-1>=0&&(map[x][y-1]=='.'||map[x][y-1]=='E')) { dfs_left(x,y-1,step+1,3); } else if(x-1>=0&&(map[x-1][y]=='.'||map[x-1][y]=='E')) { dfs_left(x-1,y,step+1,1);// } else if(y+1<w&&(map[x][y+1]=='.'||map[x][y+1]=='E')) { dfs_left(x,y+1,step+1,4); } else { if(x+1<h&&(map[x+1][y]=='.'||map[x+1][y]=='E')) dfs_left(x+1,y,step+1,2); } } else if(dir ==2) { //printf("%d 到了方向下\n",step); if(y+1<w&&(map[x][y+1]=='.'||map[x][y+1]=='E')) { dfs_left(x,y+1,step+1,4); } else if(x+1<h&&(map[x+1][y]=='.'||map[x+1][y]=='E')) { dfs_left(x+1,y,step+1,2); } else if(y-1>=0&&(map[x][y-1]=='.'||map[x][y-1]=='E')) { dfs_left(x,y-1,step+1,3); } else { if(x-1>=0&&(map[x-1][y]=='.'||map[x-1][y]=='E')) dfs_left(x-1,y,step+1,1); } } else if(dir==3) { if(x+1<h&&(map[x+1][y]=='.'||map[x+1][y]=='E')) { dfs_left(x+1,y,step+1,2); } else if(y-1>=0&&(map[x][y-1]=='.'||map[x][y-1]=='E')) { dfs_left(x,y-1,step+1,3); } else if(x-1>=0&&(map[x-1][y]=='.'||map[x-1][y]=='E')) { dfs_left(x-1,y,step+1,1); } else { if(y+1<w&&(map[x][y+1]=='.'||map[x][y+1]=='E')) dfs_left(x,y+1,step+1,4); } } else if(dir == 4) { if(x-1>=0&&(map[x-1][y]=='.'||map[x-1][y]=='E')) { dfs_left(x-1,y,step+1,1); } else if(y+1<w&&(map[x][y+1]=='.'||map[x][y+1]=='E')) { dfs_left(x,y+1,step+1,4); } else if(x+1<h&&(map[x+1][y]=='.'||map[x+1][y]=='E')) { dfs_left(x+1,y,step+1,2); } else { if(y-1>=0&&(map[x][y-1]=='.'||map[x][y-1]=='E')) dfs_left(x,y-1,step+1,3); } }}int dfs_right(int x,int y ,int step,int dir){ if(map[x][y]=='E') { return step; } if(dir==1) { if(y+1<w&&(map[x][y+1]=='.'||map[x][y+1]=='E')) { dfs_right(x,y+1,step+1,4); } else if(x-1>=0&&(map[x-1][y]=='.'||map[x-1][y]=='E')) { dfs_right(x-1,y,step+1,1); } else if(y-1>=0&&(map[x][y-1]=='.'||map[x][y-1]=='E')) { dfs_right(x,y-1,step+1,3); } else if(x+1<h&&(map[x+1][y]=='.'||map[x+1][y]=='E')) { dfs_right(x+1,y,step+1,2); } } else if(dir ==2) { if(y-1>=0&&(map[x][y-1]=='.'||map[x][y-1]=='E')) { dfs_right(x,y-1,step+1,3); } else if(x+1<h&&(map[x+1][y]=='.'||map[x+1][y]=='E')) { dfs_right(x+1,y,step+1,2); } else if(y+1<w&&(map[x][y+1]=='.'||map[x][y+1]=='E')) { dfs_right(x,y+1,step+1,4); } else if(x-1>=0&&(map[x-1][y]=='.'||map[x-1][y]=='E')) { dfs_right(x-1,y,step+1,1); } } else if(dir==3) { if(x-1>=0&&(map[x-1][y]=='.'||map[x-1][y]=='E')) { dfs_right(x-1,y,step+1,1); } else if(y-1>=0&&(map[x][y-1]=='.'||map[x][y-1]=='E')) { dfs_right(x,y-1,step+1,3); } else if(x+1<h&&(map[x+1][y]=='.'||map[x+1][y]=='E')) { dfs_right(x+1,y,step+1,2); } else if(y+1<w&&(map[x][y+1]=='.'||map[x][y+1]=='E')) { dfs_right(x,y+1,step+1,4); } } else if(dir ==4) { if(x+1<h&&(map[x+1][y]=='.'||map[x+1][y]=='E')) { dfs_right(x+1,y,step+1,2); } else if(y+1<w&&(map[x][y+1]=='.'||map[x][y+1]=='E')) { dfs_right(x,y+1,step+1,4); } else if(x-1>=0&&(map[x-1][y]=='.'||map[x-1][y]=='E')) { dfs_right(x-1,y,step+1,1); } else if(y-1>=0&&(map[x][y-1]=='.'||map[x][y-1]=='E')) { dfs_right(x,y-1,step+1,3); } }}int bfs(){ memset(vis,0,sizeof(vis)); head = tail = 0; que[tail].x = startx; que[tail].y = starty; que[tail++].step = 1; vis[startx][starty] = 1; while(head<tail) { node front = que[head]; head++; for(int i = 0;i<4;i++) { node temp; int x = front.x + dir[i][0]; int y = front.y + dir[i][1]; if(x == endx&& y == endy) { return front.step+1; } if(x>=0&x<h&&y>=0&&y<w&&!vis[x][y]&&map[x][y]!='#') { temp.x = x; temp.y = y; temp.step = front.step+1; que[tail++]=temp; vis[x][y] = 1; } } }}int main(){ //freopen("1.txt","r",stdin); scanf("%d",&T); while(T--) { scanf("%d%d",&w,&h); getchar(); input(); printf("%d %d %d\n",dfs_left(startx,starty,1,3),dfs_right(startx,starty,1,4),bfs()); } return 0;}
- uestc oj 1269 Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- Children of the Candy Corn
- pku 3083 Children of the Candy Corn
- POJ3083--Children of the Candy Corn
- 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- poj 3083Children of the Candy Corn
- POJ3083 Children of the Candy Corn 搜索
- B - Children of the Candy Corn
- uestc oj 1668 分数拆分
- JAVA 多线程之生产者与消费者
- JavaScript:作为数据的函数
- uestc oj 1490 Eight Puzzle
- C 内存分配(转载)
- uestc oj 1269 Children of the Candy Corn
- uestc oj 1002 解救小Q
- Struts2 contentType属性列表
- uestc oj 1276 Chiara’s “Beiju”
- Android倚天剑之Notification之亮剑IOS
- 扑克牌24点游戏JAVA实现
- uestc oj 1272 Sticks
- uestc oj 1488 Flip Game
- struts2文件下载出现Can not find a java.io.InputStream with the name [imageStream] in the invocation stack.