The Worm Turns
2016-03-09 19:57
316 查看
算法:搜索
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divided into cells, and each cell contains either food or a rock. Winston wanders aimlessly for a while until he gets hungry; then he immediately eats the food
in his cell, chooses one of the four directions (north, south, east, or west) and crawls in a straight line for as long as he can see food in the cell in front of him. If he sees a rock directly ahead of him, or sees a cell where he has already eaten the food,
or sees an edge of the rectangular patch, he turns left or right and once again travels as far as he can in a straight line, eating food. He never revisits a cell. After some time he reaches a point where he can go no further so Winston stops, burps and takes
a nap.
For instance, suppose Winston wakes up in the following patch of earth (X's represent stones, all other cells contain food):
If Winston starts eating in row 0, column 3, he might pursue the following path (numbers represent order of visitation):
In this case, he chose his path very wisely: every piece of food got eaten. Your task is to help Winston determine where he should begin eating so that his path will visit as many food cells as possible.
Input
Input will consist of multiple test cases. Each test case begins with two positive integers, m and n , defining the number of rows and columns of the patch of earth. Rows and columns are numbered starting at 0, as in the figures above. Following these is a
non-negative integer r indicating the number of rocks, followed by a list of 2r integers denoting the row and column number of each rock. The last test case is followed by a pair of zeros. This should not be processed. The value m×n will not exceed 625.
Output
For each test case, print the test case number (beginning with 1), followed by four values:
amount row column direction
where amount is the maximum number of pieces of food that Winston is able to eat, (row, column) is the starting location of a path that enables Winston to consume this much food, and direction is one of E, N, S, W, indicating the initial direction in which
Winston starts to move along this path. If there is more than one starting location, choose the one that is lexicographically least in terms of row and column numbers. If there are optimal paths with the same starting location and different starting directions,
choose the first valid one in the list E, N, S, W. Assume there is always at least one piece of food adjacent to Winston's initial position.
Sample Input
5 5
3
0 4 3 1 3 2
0 0
Sample Output
Case 1: 22 0 3 W
代码:
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divided into cells, and each cell contains either food or a rock. Winston wanders aimlessly for a while until he gets hungry; then he immediately eats the food
in his cell, chooses one of the four directions (north, south, east, or west) and crawls in a straight line for as long as he can see food in the cell in front of him. If he sees a rock directly ahead of him, or sees a cell where he has already eaten the food,
or sees an edge of the rectangular patch, he turns left or right and once again travels as far as he can in a straight line, eating food. He never revisits a cell. After some time he reaches a point where he can go no further so Winston stops, burps and takes
a nap.
For instance, suppose Winston wakes up in the following patch of earth (X's represent stones, all other cells contain food):
If Winston starts eating in row 0, column 3, he might pursue the following path (numbers represent order of visitation):
In this case, he chose his path very wisely: every piece of food got eaten. Your task is to help Winston determine where he should begin eating so that his path will visit as many food cells as possible.
Input
Input will consist of multiple test cases. Each test case begins with two positive integers, m and n , defining the number of rows and columns of the patch of earth. Rows and columns are numbered starting at 0, as in the figures above. Following these is a
non-negative integer r indicating the number of rocks, followed by a list of 2r integers denoting the row and column number of each rock. The last test case is followed by a pair of zeros. This should not be processed. The value m×n will not exceed 625.
Output
For each test case, print the test case number (beginning with 1), followed by four values:
amount row column direction
where amount is the maximum number of pieces of food that Winston is able to eat, (row, column) is the starting location of a path that enables Winston to consume this much food, and direction is one of E, N, S, W, indicating the initial direction in which
Winston starts to move along this path. If there is more than one starting location, choose the one that is lexicographically least in terms of row and column numbers. If there are optimal paths with the same starting location and different starting directions,
choose the first valid one in the list E, N, S, W. Assume there is always at least one piece of food adjacent to Winston's initial position.
Sample Input
5 5
3
0 4 3 1 3 2
0 0
Sample Output
Case 1: 22 0 3 W
代码:
#include <iostream> #include <cstring> #include <iomanip> #include <string> #include <algorithm> using namespace std; int a[700][700],b[700][700],n,m,Max; int c[4][2]={0,1,-1,0,1,0,0,-1}; char ch[4]={'E','N','S','W'}; int ax,ay,az,bx,by,bz; void dfs(int cx,int cy,int cz,int step) { if(step>Max) { bx=ax; bz=az; by=ay; Max=step; } int dx=cx+c[cz][0]; int dy=cy+c[cz][1]; if(dx>=0&&dx<n&&dy>=0&&dy<m&&!a[dx][dy]&&!b[dx][dy]) { b[dx][dy]=1; dfs(dx,dy,cz,step+1); b[dx][dy]=0; } else { if(step==0) return ; for(int i=0;i<4;i++) { if(i==cz) continue; dx=cx+c[i][0]; dy=cy+c[i][1]; if(dx>=0&&dx<n&&dy>=0&&dy<m&&!a[dx][dy]&&!b[dx][dy]) { b[dx][dy]=1; dfs(dx,dy,i,step+1); b[dx][dy]=0; } } } } int main() { int i,j,k,q=0,p; while(scanf("%d%d",&n,&m)&&n&&m) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); Max=-1; scanf("%d",&p); while(p--) { scanf("%d%d",&i,&j); a[i][j]=1; } for(i=0;i<n;i++) { for(j=0;j<m;j++) { for(k=0;k<4;k++) { if(!a[i][j]) { ax=i; ay=j; az=k; b[i][j]=1; dfs(i,j,k,0); b[i][j]=0; } } } } cout<<"Case "<<++q<<": "<<Max+1<<" "<<bx<<" "<<by<<" "<<ch[bz]<<endl; } return 0; }
相关文章推荐
- iOS黑魔法-Method Swizzling
- 作业1
- winform textBox输入提示
- 蓝桥杯--2012--古堡算式(暴力)
- Android适配器详解
- Codeforces 631E:Product Sum
- 第二周项目简短程序
- CSS预处理器Sass(Scss)、Less、Stylus
- 为什么要多线程开发
- 蓝桥杯--2012--古堡算式(暴力)
- bzoj 1367
- Xcode重构功能怎么用我全告诉你
- HDU2519新生晚会(组合数公式)
- 生成前N个自然数的一个随机置换
- js中如何将字符串转化为时间,并计算时间差
- Struts2基础学习总结
- 二维线段树模板题 hdu 4819
- 第二周项目简短程序
- Objective-C与JavaScript交互的那些事
- 从源代码分析Android-Universal-Image-Loader的缓存处理机制