POJ 3050 Hopscotch
http://poj.org/problem?id=3050
深搜 + set去重
这道题的深搜很简单 每满六次后 insertin进set即可 最终果就是set.size()
1 #include <iostream> 2 #include <fstream> 3 #include <stdio.h> 4 #include <string> 5 #include <set> 6 7 using namespace std; 8 9 string srec; 10 char rec[8]; 11 int d[4][2] = { {-1,0}, {0, 1}, {1, 0}, {0, -1} }; 12 int grid[8][8]; 13 14 set<string> str_set; 15 16 bool check(int x, int y) 17 { 18 if (x < 0 || y < 0 || x >= 5 || y >= 5) return false; 19 return true; 20 } 21 22 void dfs(int x, int y, int n)//深搜的话 O(n^2*4^5*nlogn) --->>> 25*4^5*nlogn 数据小还好 用set消重 23 { 24 if (n == 6) 25 { 26 srec = rec; 27 str_set.insert(srec); 28 return ; 29 } 30 rec[n] = grid[x][y] + '0';//记录这个格子里的数 31 for (int i = 0; i < 4; i++) 32 { 33 if (check(x+d[i][0], y+d[i][1]))//越界检查 34 { 35 dfs(x+d[i][0], y+d[i][1], n+1); 36 } 37 } 38 } 39 40 int main() 41 { 42 freopen("in.txt", "r", stdin); 43 ofstream cout ("out.txt"); 44 for (int i = 0; i < 5; i++) 45 { 46 for (int j = 0;j < 5; j++) 47 { 48 scanf("%d", &grid[i][j]); 49 } 50 } 51 for (int i = 0; i < 5; i++) 52 { 53 for (int j = 0; j < 5; j++) 54 { 55 dfs(i,j,0); 56 } 57 } 58 printf("%d", str_set.size()); 59 return 0; 60 }