标签:dfs
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2156 | Accepted: 1548 |
Description
Input
Output
Sample Input
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1
Sample Output
15
Hint
代码:
#include <cstdio>
#include <cstring>
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int ans, num, map[10][10];
bool vis[1000000];
void dfs(int x, int y, int cur, int step){
if(step == 6){
if(!vis[cur]){
vis[cur] = 1;
++ans;
}
return ;
}
int i;
for(i = 0; i < 4; i ++){
int nx = x+dx[i]; int ny = y+dy[i];
if(nx<0||nx>=5||ny<0||ny>=5) continue;
dfs(nx, ny, cur*10+map[nx][ny], step+1);
}
}
void sol(){
ans = 0;
int i, j;
for(i = 0; i < 5; i ++){
for(j = 0; j < 5; j ++){
//printf("fdjkl");
dfs(i, j, map[i][j], 1);
}
}
}
int main(){
int i, j;
for(i = 0; i < 5; i ++)
for(j = 0; j < 5; j ++)
scanf("%d", &map[i][j]);
// printf("dfgjk\n");
sol();
printf("%d\n", ans);
return 0;
}标签:dfs
原文地址:http://blog.csdn.net/shengweisong/article/details/41775025