HUST 1343 C - Coil Judge (哈理工 亚洲区选拔赛前练习赛)

时间:2023-02-13 20:50:30
C - Coil Judge
Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu

Description

Mortal Coil is a very popular game in the past few months. The game has a rectangular maze with obstacles. You guide a virtual snake. The game goes like this: 1. Choose a grid as starting point of the snake. (Then the grid is marked blocked) 2. Choose a direction (Above Down Left Right) to let the snake go along it until it is blocked by original obstacles or its own body or the border of the maze. The grid which the snake has ever passed would never allow passing any more. 3. If all the grids is occupied by original obstacles or the snake’s body, you win, otherwise repeat procedure 2 and move on until you win or you haven’t choice to move, i.e. you lose. HUST 1343 C - Coil Judge (哈理工 亚洲区选拔赛前练习赛) Your mission is simple——given a maze and a solution, you are asked to judge whether the solution is correct. If the solution is incorrect, please point out whether it tries to step into forbidden grid, it finishes all steps but doesn’t visit all the free grids or the starting point is valid.

Input

The input contains multiple cases ended with EOF. For each case, first line contains two integers: R, C. (3<=R, C<=10) The next R lines each contains C characters——‘X’ or ‘.’ .’X’ represents obstacles and ‘.’ represents free grids. The next line contains two integers: Y, X, i.e. the snake begins at grid (Y, X) The next line contains a string contains only characters in {U, D, L, R}, meaning “up down left right” in order. Is has at most 1000 and at least 1 characters. This line and the above line describe the solution.

Output

For each case output the result in one line. There are 4 kinds of possible outcomes. You can refer to the Sample Output.

Sample Input

3 5
.....
.X...
...X.
2 4
ULDRUR

3 5
.....
.X...
...X.
1 1
ULDRUR

3 5
.....
.X...
...X.
2 4
ULDR

3 5
.....
.X...
...X.
2 4
ULDRUL

Sample Output

Case #1: correct
Case #2: invalid starting point  
Case #3: 2 more free grid(s)
Case #4: invalid move at (1,2)




HINT
If there are redundant moves after you won, the solution is considered as invalid move like Case #4. 


我们队今天挂了15发。。坑在哪儿呢! 在于输入的时候起点可能并不在n * m 范围内!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 吐血了~~~~~~~~~~~ 
/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-08-30 22:26
#
#     Filename: A.cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/
#


#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 34;
char cnt[INF][INF];
int main()
{
    int n, m;
    int cs = 1;
    while(scanf("%d%d",&n, &m) != EOF)
    {
        memset(cnt, '\0', sizeof(cnt));
        int s1 = 1,s = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%s",cnt[i]);
            for(int j = 0; j < m; j++)
            {
                if(cnt[i][j] == '.')
                {
                    s++;
                }
            }
        }
        int x, y;
        scanf("%d%d",&x, &y);
        string str;
        cin>>str;
        if(cnt[x][y] == 'X' || (x < 0 )||x >= n || y < 0 || y >= m)
        {
            printf("Case #%d: invalid starting point\n",cs++);
            continue;
        }
        cnt[x][y] = '*';
        int flag = 0;
        for(int kk = 0; kk < str.size(); kk++)
        {
            if(str[kk] == 'U')
            {
                int x1;
                for(int j = 1; ; j ++)
                {
                    x1 = x - j;
                    if(x1 < 0)
                    {
                        if(j == 1)
                        {

                            printf("Case #%d: invalid move at (%d,%d)\n",cs++,x1 + 1, y);
                            flag = 1;

                        }
                        break;
                    }
                    else if(cnt[x1][y] == 'X' )
                    {
                        if(j == 1)
                        {
                            printf("Case #%d: invalid move at (%d,%d)\n",cs++,x1 + 1, y);
                            flag = 1;

                        }
                        break;
                    }
                    else if(cnt[x1][y] == '*')
                    {
                        if(j == 1)
                        {
                            printf("Case #%d: invalid move at (%d,%d)\n",cs++,x1 + 1, y);
                            flag = 1;

                        }
                        break;
                    }

                    else
                    {
                        s1++;
                        cnt[x1][y] =  '*';
                    }
                }
                x = x1 + 1;
            }
            else if(str[kk] == 'L')
            {
                int y1;
                for(int j = 1; ; j ++)
                {
                    y1 = y - j;
                    if(y1 < 0 || cnt[x][y1] == 'X' || cnt[x][y1] == '*')
                    {
                        if(j == 1)
                        {
                            printf("Case #%d: invalid move at (%d,%d)\n",cs++,x, y1 + 1);
                            flag = 1;

                        }
                        break;
                    }
                    else
                    {
                        s1++;
                        cnt[x][y1] =  '*';
                    }
                }
                y = y1 + 1;
            }
            else if(str[kk] == 'D')
            {
                int x1;
                for(int j = 1; ; j ++)
                {
                    x1 = x + j;
                    if(x1 >= n || cnt[x1][y] == 'X' ||cnt[x1][y] == '*')
                    {
                        if(j == 1)
                        {
                            printf("Case #%d: invalid move at (%d,%d)\n",cs++,x1 - 1, y);
                            flag = 1;

                        }
                        break;
                    }
                    else
                    {
                        cnt[x1][y] =  '*';
                        s1++;
                    }
                }
                x = x1 - 1;
            }
            else if(str[kk] == 'R')
            {
                int y1;
                for(int j = 1; ; j ++)
                {
                    y1 = y + j;
                    if(y1 >= m || cnt[x][y1] == 'X' || cnt[x][y1] == '*')
                    {
                        if(j == 1)
                        {

                            printf("Case #%d: invalid move at (%d,%d)\n",cs++,x, y1 - 1);
                            flag = 1;

                        }
                        break;
                    }
                    else
                    {
                        s1++;
                        cnt[x][y1] =  '*';
                    }
                }
                y = y1 - 1;
            }
            if(flag)
                break;
        }
        if(flag)continue;
        if(s == s1)
        {
            printf("Case #%d: correct\n",cs++);
        }
        else
            printf("Case #%d: %d more free grid(s)\n",cs++,s - s1);
    }
    return 0;
}

/*
3 6
......
......
..X...
2 5
ULDRURD
3 3
X.X
X.X
X.X
2 1
UU
3 5
.....
.X...
...X.
2 4
ULDRURD

3 5
.....
.X...
...X.
1 1
ULDRUR

3 5
.....
.X...
...X.
2 4
ULDR

3 5
.....
.X.X.
...X.
2 4
ULDRUL
*/