#include< bits/stdc++.h >
using namespace std;
int data[21][21];
int dfs(int u,int v,int n){
int ans = 0;
if( u == n and v == n) return 1;
data[u][v] = 1;
if(u - 1 >= 1 and data[u-1][v] == 0)
ans += dfs(u-1,v,n);
if(v - 1 >= 1 and data[u][v-1] == 0)
ans += dfs(u,v-1,n);
if(u+1 <= n and data[u+1][v] == 0)
ans += dfs(u+1,v,n);
if(v+1 <= n and data[u][v+1] == 0)
ans += dfs(u,v+1,n);
data[u][v] = 0;
return ans ;
}
int main()
{
freopen("in.txt","r",stdin);
int n, t;
cin >> t;
while(t--){
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> data[i][j];
if(data[1][1] == 1 and data[n][n] == 1) cout << 0 << endl;
else cout << dfs(1,1,n) << endl;
}
return 0;
}
using namespace std;
int data[21][21];
int dfs(int u,int v,int n){
int ans = 0;
if( u == n and v == n) return 1;
data[u][v] = 1;
if(u - 1 >= 1 and data[u-1][v] == 0)
ans += dfs(u-1,v,n);
if(v - 1 >= 1 and data[u][v-1] == 0)
ans += dfs(u,v-1,n);
if(u+1 <= n and data[u+1][v] == 0)
ans += dfs(u+1,v,n);
if(v+1 <= n and data[u][v+1] == 0)
ans += dfs(u,v+1,n);
data[u][v] = 0;
return ans ;
}
int main()
{
freopen("in.txt","r",stdin);
int n, t;
cin >> t;
while(t--){
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> data[i][j];
if(data[1][1] == 1 and data[n][n] == 1) cout << 0 << endl;
else cout << dfs(1,1,n) << endl;
}
return 0;
}
No comments:
Post a Comment