Time Limit: 4000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

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

2008 East Central Regional Contest

AC代码：

#include <cstdio>
#include <cstring>
using namespace std;
int m, n, r;
bool ok;
int ans, ansx, ansy;
char ansdir;
const int dx[] = {0, 0, 1, -1},
dy[] = {1, -1, 0, 0};
const char dirch[] = "EWSN";
int map[630][630];
int T = 1;
void init()
{
memset(map, 0, sizeof(map));
ans = 0;
}
void input()
{
int x, y;
scanf("%d", &r);
for (int i = 0; i < r; i++)
{
scanf("%d%d", &x, &y);
map[x][y] = 1;
}
}
bool check(int x, int y)
{
if (x < 0 || x >= m || y < 0 || y >= n || map[x][y])
return 0;
return 1;
}
void dfs(int x, int y, int step, int dir)
{
int newx, newy;
bool final = 1;
newx = x + dx[dir];
newy = y + dy[dir];
if (check(newx, newy))
{
final = 0;
map[newx][newy] = 1;
dfs(newx, newy, step + 1, dir);
map[newx][newy] = 0;
}
else
{
if (dir == 0 || dir == 1)
{
newx = x + dx[2];
newy = y + dy[2];
if (check(newx, newy))
{
final = 0;
map[newx][newy] = 1;
dfs(newx, newy, step + 1, 2);
map[newx][newy] = 0;
}
newx = x + dx[3];
newy = y + dy[3];
if (check(newx, newy))
{
final = 0;
map[newx][newy] = 1;
dfs(newx, newy, step + 1, 3);
map[newx][newy] = 0;
}
}
else
{
newx = x + dx[0];
newy = y + dy[0];
if (check(newx, newy))
{
final = 0;
map[newx][newy] = 1;
dfs(newx, newy, step + 1, 0);
map[newx][newy] = 0;
}
newx = x + dx[1];
newy = y + dy[1];
if (check(newx, newy))
{
final = 0;
map[newx][newy] = 1;
dfs(newx, newy, step + 1, 1);
map[newx][newy] = 0;
}
}
}
if (final && step > ans)
{
ok = 1;
ans = step;
}
}
void solve()
{
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (!map[i][j])
{
map[i][j] = 1;
for (int k = 0; k < 4; k++)
if (check(i + dx[k], j + dy[k]))
{
ok = 0;
dfs(i, j, 1, k);
if (ok)
ansx = i, ansy = j, ansdir = dirch[k];
}
map[i][j] = 0;
}
printf("Case %d: %d %d %d %c\n", T++, ans, ansx, ansy, ansdir);
}
int main()
{
while (scanf("%d%d", &m, &n) == 2 && m && n)
{
init();
input();
solve();
}
return 0;
}