(搜索)跳棋系列3

来源:互联网 发布:删除表中数据 编辑:程序博客网 时间:2024/04/28 17:41
跳棋#3
TimeLimit: 1 Second MemoryLimit: 32 Megabyte

Description


大家都玩过跳棋的游戏吧,正常的跳棋游戏能隔子往6个方向跳,在本题中只能隔一个子往4个 方向跳,即前后左右4个方向。还可以挪步,即往4个方向走1步,步数不限。 如图,3代表要跳的棋子,2代表普通的棋子,1代表棋子3能跳到的位置,0代表空地。 1 1 1 1 1 1 1 2 1 1 1 2 3 2 1 1 1 2 1 1 1 1 1 1 1 现在需要你设计程序求出3能跳到的位置。


Input

有多组数据,每组数据第一行是两个整数5<=n,m<=100,接下来n行每行m个数,保证只 有一个3,其余的只有2和0。数字之间有空格。


Output

输出n行每行m个数,代表棋盘的状态,数字与数字之间要求有空格,每行最后一个数字后不 许有空格。


Sample Input


5 5
0 0 2 2 0
2 0 2 0 2
0 2 0 0 2
2 3 2 0 2
0 2 2 0 0


Sample Output


1 1 2 2 0
2 1 2 1 2
1 2 1 1 2
2 3 2 1 2

1 2 2 1 1


#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 110;int Graph[MAXN][MAXN];int n, m;void DFS(int a, int b){    Graph[a][b] = 1;    if(a >= 2 && Graph[a-1][b] == 2 && Graph[a-2][b] == 0)        DFS(a-2, b);    if(a+2 < n && Graph[a+1][b] == 2 && Graph[a+2][b] == 0)        DFS(a+2, b);    if(b >= 2 && Graph[a][b-1] == 2 && Graph[a][b-2] == 0)        DFS(a, b-2);    if(b+2 < m && Graph[a][b+1] == 2 && Graph[a][b+2] == 0)        DFS(a, b+2);    if(a >=1 && Graph[a-1][b] == 0)        DFS(a-1, b);    if(a+1 < n && Graph[a+1][b] == 0)        DFS(a+1, b);    if(b+1 < m && Graph[a][b+1] == 0)        DFS(a, b+1);    if(b >= 1 && Graph[a][b-1] == 0)        DFS(a, b-1);}int main(){    while(~scanf("%d %d", &n, &m))    {        int i, j, iStartx, iStarty;        memset(Graph, 0, sizeof(Graph));        for(i = 0; i < n; ++i)        {            for(j = 0; j < m; ++j)            {                scanf("%d", &Graph[i][j]);                if(Graph[i][j] == 3)                    iStartx = i, iStarty = j;            }        }        DFS(iStartx, iStarty);        Graph[iStartx][iStarty] = 3;        for(i = 0; i < n; ++i)        {            for(j = 0; j < m-1; ++j)                printf("%d ", Graph[i][j]);            printf("%d\n", Graph[i][m-1]);        }    }    return 0;}


0 0