Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6761 | Accepted: 4354 |
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
题意:给一个5x5的矩阵,从矩阵的任意一个位置开始搜索6次,(允许方向为上下左右四个方向),问一可以产生多少个不同的序列
#include<iostream> #include<string.h> #include<string> #include<algorithm> #include<queue> #include<set> using namespace std; char a[10][10]; int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; set<string>p; string s; int check(int x,int y) { if(x>=0&&x<5&&y>=0&&y<5) return 1; else return 0; } void dfs(int x,int y,int n) { if(n>=6) { p.insert(s); return ; } for(int i=0;i<4;i++) { int dx,dy; dx=x+dir[i][0]; dy=y+dir[i][1]; if(check(dx,dy)==0) continue; s=s+a[dx][dy]; dfs(dx,dy,n+1); s.erase(n); } return ; } int main() { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) cin>>a[i][j]; } for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { s.clear(); dfs(i,j,0); } } cout<<p.size()<<endl; // set<string>::iterator it; // for(it=p.begin();it!=p.end();it++) // { // cout<<*it<<endl; // } }
原文:https://www.cnblogs.com/-citywall123/p/11295587.html