UVa10452 - Marcus

#include <cstdio>
#include <cstring>
#include <map>
#include <string>

using namespace std;

const int N = 10;
const int M = 3;
const char path[] = "@IEHOVA#";
const int dx[M] = {-1, 0, 0};
const int dy[N] = {0, -1, 1};

char g[N][N];
int n, m;
int startx, starty;
bool vis[N][N];
map<int, string> dirMap;
bool first;

void dfs(int x, int y, int dep);

int main()
{
    int t;

#ifndef ONLINE_JUDGE
    freopen("uva_in.txt", "r", stdin);
#endif

    dirMap[0] = string("forth");
    dirMap[1] = string("left");
    dirMap[2] = string("right");

    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        gets(g[0]);
        for (int i = 0; i < n; i++) {
            gets(g[i]);
            for (int j = 0; g[i][j]; j++) {
                if (g[i][j] == '@') {
                    startx = i;
                    starty = j;
                }
            }
        }
        memset(vis, false, sizeof(vis));
        first = true;
        dfs(startx, starty, 0);
        printf("\n");
    }
    return 0;
}

void dfs(int x, int y, int dep)
{
    if (vis[x][y]) return;
    if (x < 0 || x >= n || y < 0 || y >= m) return;
    
    if (dep >= 7) return;

    if (g[x][y] == path[dep]) {
        vis[x][y] = true;

        for (int i = 0; i < M; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (g[xx][yy] == path[dep + 1]) {
                //printf("%s", dirMap[i].c_str()) ;
                if (first) first = false;
                else printf(" ");
                printf("%s", dirMap[i].c_str());
                dfs(xx, yy, dep + 1);
            }        
        }
    }
}

你可能感兴趣的:(UVa10452 - Marcus)