# The Worm Turns

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1162    Accepted Submission(s): 439

Problem Description
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

Source
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 630;
int n, m, r;
int mp[maxn][maxn],visit[maxn][maxn];
int dir[4][2] = {{0,1},{-1,0},{1,0},{0,-1}};
char dirFlag[4] = {'E', 'N', 'S', 'W'};
int maxLen;
int sX, sY, sD, eX, eY, eD;
void dfs(int x,int y,int d,int step)
{
if(step > maxLen)
{
eX = sX;
eY=  sY;
eD = sD;
maxLen = step;
}

int dx = x + dir[d][0];
int dy = y + dir[d][1];

if(dx < 0 || dy < 0 || dx >= m || dy >= n || mp[dx][dy] == 1 || visit[dx][dy] == 1)
{
if(step == 0) return ;  //步数为0的时候不允许转向

for(int i = 0;i < 4;i ++)
{
if(i == d) continue;

dx = x + dir[i][0];
dy = y + dir[i][1];
if(dx >= 0 && dx < m && dy >= 0 && dy < n && mp[dx][dy] == 0 && visit[dx][dy] == 0)
{
visit[dx][dy] = 1;
dfs(dx,dy,i,step + 1);
visit[dx][dy] = 0;
}
}
}
else
{
visit[dx][dy] = 1;
dfs(dx,dy,d,step + 1);
visit[dx][dy] = 0;
}
}

int main()
{
int cnt = 0;
while(cin >> m >> n && m && n)
{
cnt ++;
maxLen = -1;
memset(mp,0,sizeof(mp));
memset(visit,0,sizeof(visit));
cin >> r;
int dx, dy;
for(int i = 1;i <= r;i ++)
{
cin >> dx >> dy;
mp[dx][dy] = 1;  //岩石
}

for(int i = 0;i < m;i ++)
{
for(int j = 0;j < n;j ++)
{
for(int k = 0;k < 4;k ++)
{
if(!mp[i][j])
{
sX = i;
sY = j;
sD = k;
visit[i][j] = 1;
dfs(i,j,k,0);
visit[i][j] = 0;
}
}
}
}
printf("Case %d: %d %d %d %c\n",cnt,maxLen + 1,eX,eY,dirFlag[eD]);
}
return 0;
}