Saturday, January 4, 2020

POJ.3050 Hopscotch

Problem:
http://poj.org/problem?id=3050

Idea:
Using dfs for exhaustive search.

Source:
 #include<cstdio>  
 #include<queue>  
 #include<string>  
 #include<iostream>  
 #include<vector>  
 #include<set>  
 using namespace std;  
 char g[5][5];  
 set<string> ans;  
 int dx[5] = { 0,0,-1,1};  
 int dy[5] = { -1,1,0,0};  
 void dfs(int i, int j, string res, int cur)  
 {  
      if (cur == 6) {  
           ans.insert(res);  
           return;  
      }  
      for (int k = 0; k < 4; k++) {  
           int nx = i + dx[k];  
           int ny = j + dy[k];  
           if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 ) {  
                dfs(nx, ny, res + g[nx][ny], cur + 1);  
           }  
      }  
      return;  
 }  
 int main()  
 {  
      for (int i = 0; i < 5; i++)  
           for (int j = 0; j < 5; j++) {  
                //scanf("%c", &g[i][j]);  
                cin >> g[i][j];  
           }  
      for (int i = 0; i < 5; i++)  
           for (int j = 0; j < 5; j++) {  
                dfs(i, j, "", 0);  
           }  
      cout << ans.size() << endl;  
      return 0;  
 }  

No comments:

Post a Comment