标签:count psc set queue 答案 while sof ring sum
bfs限制步数后爆搜就行了,答案用set维护(用来去重).
1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio> 5 #include <cmath> 6 #include <cstring> 7 #include <string> 8 #include <vector> 9 #include <stack> 10 #include <queue> 11 #include <map> 12 #include <set> 13 14 using namespace std; 15 typedef long long LL; 16 const int INF_INT=0x3f3f3f3f; 17 const LL INF_LL=0x3f3f3f3f3f3f3f3f; 18 19 struct G 20 { 21 int x,y,cnt,sum; 22 }; 23 24 int grid[5][5]; 25 set<int> ans; 26 int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; 27 int res=0; 28 29 void bfs(int s,int t) 30 { 31 queue<G> que; 32 que.push((G){s,t,0,grid[s][t]}); 33 while(que.size()) 34 { 35 G g=que.front();que.pop(); 36 if(g.cnt==5) 37 { 38 if(!ans.count(g.sum)) ans.insert(g.sum),res++; 39 continue; 40 } 41 for(int i=0;i<4;i++) 42 { 43 int nx=g.x+dx[i],ny=g.y+dy[i]; 44 if(nx>=0&&nx<5&&ny>=0&&ny<5) 45 que.push((G){nx,ny,g.cnt+1,g.sum*10+grid[nx][ny]}); 46 } 47 } 48 } 49 50 int main() 51 { 52 // freopen("black.in","r",stdin); 53 // freopen("black.out","w",stdout); 54 for(int i=0;i<5;i++) 55 for(int j=0;j<5;j++) 56 cin>>grid[i][j]; 57 for(int i=0;i<5;i++) 58 for(int j=0;j<5;j++) 59 bfs(i,j); 60 cout<<res<<endl; 61 return 0; 62 }
标签:count psc set queue 答案 while sof ring sum
原文地址:https://www.cnblogs.com/VBEL/p/11396937.html