Test instructions: On a 5*5 map, jump five times from any position up or down, forming a number. Q: How many non-repeating numbers are there?
Idea: DFS
- Jump 5 times from any location, indicating that each location requires traversal.
- form a number: Number*10+map[dx][dy]
- A number that is not duplicated, stored with set (set)
- Only need to jump each time the number of steps plus 1, and can jump position, as long as no more than the range can be, that a position can be repeated jump
Code to solve the problem:
#include <iostream>#include<cstdio>#include<Set>using namespacestd;intmap[5][5];Set<int>results;Const intdir[4][2]{ { 0,1},{0,-1},{1,0},{ -1,0 }};voidDfsConst int& X,Const int& Y,Const int& Step,Const int&Number ) { if(Step = =5) {Results.insert (number); return; } for(inti =0; I <4; i++) { intDX = x + dir[i][0]; intDy = y + dir[i][1]; if(DX >=0&& DX <5&& Dy >=0&& Dy <5) Dfs (dx, dy, step+1, number *Ten+Map[dx][dy]); }}intMain () { for(inti =0; I <5; i++) for(intj =0; J <5; J + +) scanf ("%d", &Map[i][j]); for(inti =0; I <5; i++) for(intj =0; J <5; J + +) Dfs (i, J,0, Map[i][j]); printf ("%d\n", Results.size ()); return 0;}
POJ 3050 Map 5-bit number problem DFS algorithm