poj1573

简单题

View Code
//zoj1708

#include <iostream>

using namespace std;



const    int        maxn = 11;



char    order[maxn][maxn];

int        steps[maxn][maxn], n, m, start;



void init()

{

    int        i, j;

    

    memset(steps, 0, sizeof(steps));

    for (i = 0; i < n; i++)

        for (j = 0; j < m; j++)

            cin >> order[i][j];

}



void work()

{

    int        x, y, now;

    

    x = 0;

    y = start - 1;

    now = 1;

    steps[x][y] = now;

    while (1)

    {

        switch (order[x][y])

        {

            case    'N':x--;break;

            case    'W':y--;break;

            case    'S':x++;break;

            case    'E':y++;break;

        }

        if (x < 0 || x >= n || y < 0 || y >= m)

        {

            cout << now << " step(s) to exit\n";

            return;

        }

        now++;

        if (steps[x][y] != 0)

        {

            cout << steps[x][y] - 1 << " step(s) before a loop of " << now - steps[x][y] << " step(s)\n";

            return;

        }

        steps[x][y] = now;

    }

}



int main()

{

    //freopen("t.txt", "r", stdin);

    cin >> n >> m >> start;

    while (!(n == 0 && m == 0 && start == 0))

    {

        init();

        work();

        cin >> n >> m >> start;

    }

    return 0;

}

 

你可能感兴趣的:(poj)