pku 3083 Children of the Candy Corn
来源:互联网 发布:神经网络的优缺点 知乎 编辑:程序博客网 时间:2024/05/16 19:46
题目要求沿左墙壁、有墙壁和最短路程分别多少。一开始不清楚怎么实现沿左(右)墙壁。后来实现方法是:在当前方向上,先看是否能向左(右)走,不行则继续向前,还不行就继续顺时针(逆时针)选择方向。
- #include <stdio.h>
- #include <string.h>
- int right[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
- int left[4][2] = { {1,0},{0,-1},{-1,0},{0,1} };
- char map[45][45];
- int si, sj, ei, ej;
- int dir;
- int q[2500][2];
- int flag[45][45];
- int n, m;
- int dfs_left(int ni, int nj, int step)
- {
- if ( ni==ei && nj==ej )
- return step+1;
- if ( ni<0 || ni>=n || nj<0 || nj>=m )
- return 0;
- if ( map[ni][nj]=='#' )
- return 0;
- dir = (dir+3)%4;
- while ( 1 )
- {
- int tmp = dfs_left( ni+left[dir][0], nj+left[dir][1], step+1 );
- if ( tmp>0 )
- break;
- dir = (dir+1)%4;
- }
- }
- int dfs_right(int ni, int nj, int step)
- {
- if ( ni==ei && nj==ej )
- return step+1;
- if ( ni<0 || ni>=n || nj<0 || nj>=m )
- return 0;
- if ( map[ni][nj]=='#' )
- return 0;
- dir = (dir+3)%4;
- while ( 1 )
- {
- int tmp = dfs_right( ni+right[dir][0], nj+right[dir][1], step+1 );
- if ( tmp>0 )
- break;
- dir = (dir+1)%4;
- }
- }
- int bfs()
- {
- memset( flag, 0, sizeof(flag) );
- int head = 0;
- int tail = 0;
- q[tail][0] = si;
- q[tail++][1] = sj;
- flag[si][sj] = 1;
- int step = 0;
- while ( head<tail && flag[ei][ej]==0 )
- {
- int tmp = tail;
- step++;
- while ( head<tmp && flag[ei][ej]==0 )
- {
- int ni = q[head][0];
- int nj = q[head++][1];
- for ( int i=0; i<4; i++ )
- {
- int ti = ni+left[i][0];
- int tj = nj+left[i][1];
- if ( ti>=0 && ti<n && tj>=0 && tj<m && flag[ti][tj]==0
- && map[ti][tj]!='#' )
- {
- q[tail][0] = ti;
- q[tail++][1] = tj;
- flag[ti][tj] = 1;
- }
- }
- }
- }
- return step+1;
- }
- int main ()
- {
- int t;
- scanf("%d", &t );
- while ( t-- )
- {
- scanf("%d %d", &m, &n );
- for ( int i=0; i<n; i++ )
- {
- scanf("%s", map[i] );
- for ( int j=0; j<m; j++ )
- {
- if ( map[i][j] =='S' )
- si = i, sj=j;
- if ( map[i][j] =='E' )
- ei = i, ej= j;
- }
- }
- dir = 0;
- printf("%d %d %d/n", dfs_left(si,sj,0), dfs_right(si,sj,0), bfs() );
- }
- return 0;
- }
- pku 3083 Children of the Candy Corn
- pku 3083 Children of the Candy Corn DFS+BFS
- 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- poj 3083Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- POJ 3083 - Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- 将代码注入到进程的三种方式
- [SharePoint]设置问卷调查权限
- Dreamweaver 粗略的理一边(也是参考“建站学”网的建站入门)
- 扫雷 源码
- 用C/C++函数调用php功能的问题
- pku 3083 Children of the Candy Corn
- TPL: 一个新的正则表达式(regex)库
- 使用CreateProcess函数运行其他程序
- JAVA面试题解惑系列(一)——类的初始化顺序
- JAVA面试题解惑系列(二)——到底创建了几个String对象?
- PKU 2513 Colored Sticks
- IIS 无法启动:发生意外错误0x8ffe2740 的原因 (感谢AICI大侠的文章)
- JAVA面试题解惑系列(三)——变量(属性)的覆盖
- JAVA面试题解惑系列(四)——final、finally和finalize的区别