資料結構實驗之棧與佇列十 走迷宮

2021-08-09 08:38:32 字數 1157 閱讀 7476

time limit: 1000ms

memory limit: 65536kb

submit

statistic

problem description

乙個由n * m 個格仔組成的迷宮,起點是(1, 1), 終點是(n, m),每次可以向上下左右四個方向任意走一步,並且有些格仔是不能走動,求從起點到終點經過每個格仔至多一次的走法數。

input

第一行乙個整數t 表示有t 組測試資料。(t <= 110)

對於每組測試資料:

第一行兩個整數n, m,表示迷宮有n * m 個格仔。(1 <= n, m <= 6, (n, m) !=(1, 1) ) 接下來n 行,每行m 個數。其中第i 行第j 個數是0 表示第i 行第j 個格仔可以走,否則是1 表示這個格仔不能走,輸入保證起點和終點都是都是可以走的。

任意兩組測試資料間用乙個空行分開。

output

對於每組測試資料,輸出乙個整數r,表示有r 種走法。

example input

3

2 20 1

0 02 2

0 11 0

2 30 0 0

0 0 0

example output

1

04

老師講了用棧的方法做,但是沒有寫出來。

其實就是從起點處,挨個方向遍歷,如果走不通,就換另乙個方向,四個方向都已經走不通了就退回前乙個格仔,最終如果能走到目標就是可行的,否則沒有一條通路。

#include#includeusing namespace std;

int n, m;

int step = 0;

int book[1000][1000], e[1000][1000];

void dfs(int x, int y)

,,,};

int tx, ty;

if(x == n&&y == m)

for(int i = 0;i<4;i++)

}return;

}int main()

}book[1][1] = 1;

dfs(1, 1);

printf("%d\n", step);

}}

資料結構實驗之棧與佇列十 走迷宮

time limit 1000ms memory limit 65536kb submit statistic problem description 乙個由n m 個格仔組成的迷宮,起點是 1,1 終點是 n,m 每次可以向上下左右四個方向任意走一步,並且有些格仔是不能走動,求從起點到終點經過每個...

資料結構實驗之棧與佇列十 走迷宮

problem description 乙個由n m 個格仔組成的迷宮,起點是 1,1 終點是 n,m 每次可以向上下左右四個方向任意走一步,並且有些格仔是不能走動,求從起點到終點經過每個格仔至多一次的走法數。input 第一行乙個整數t 表示有t 組測試資料。t 110 對於每組測試資料 第一行兩...

資料結構實驗之棧與佇列十 走迷宮

time limit 1000ms memory limit 65536kb problem description 乙個由n m 個格仔組成的迷宮,起點是 1,1 終點是 n,m 每次可以向上下左右四個方向任意走一步,並且有些格仔是不能走動,求從起點到終點經過每個格仔至多一次的走法數。input ...