来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=4&problem=227&mosmsg=Submission+received+with+ID+14026069
The House Of Santa Claus
In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do you remember that the importance was on drawing the house in a stretch without lifting the pencil and not drawing a line twice? As a reminder it has to look like shown in Figure 1.
Figure: The House of Santa Claus
Well, a couple of years later, like now, you have to ``draw'' the house again but on the computer. As one possibility is not enough, we require all the possibilities when starting in the lower left corner. Follow the example in Figure 2 while defining your stretch.
Figure: This Sequence would give the Outputline 153125432
All the possibilities have to be listed in the outputfile by increasing order, meaning that 1234... is listed before 1235... .
Output
So, an outputfile could look like this:
12435123
13245123
...
15123421
The House Of Santa Claus
In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do you remember that the importance was on drawing the house in a stretch without lifting the pencil and not drawing a line twice? As a reminder it has to look like shown in Figure 1.
The House Of Santa Claus
Figure: The House of Santa Claus
Well, a couple of years later, like now, you have to ``draw'' the house again but on the computer. As one possibility is not enough, we require all the possibilities when starting in the lower left corner. Follow the example in Figure 2 while defining your stretch.
Figure: This Sequence would give the Outputline 153125432
All the possibilities have to be listed in the outputfile by increasing order, meaning that 1234... is listed before 1235... .
Output
So, an outputfile could look like this:
12435123 13245123 ... 15123421
题意: 一笔画问题。从1开始画,输出所有可能的情况
题解: DFS 只要用个数组 path保存下路径就可以了~~
AC代码:
#include<iostream> using namespace std; bool edge[6][6]={0},visit[6][6]; int path[10]; void dfs(int pos,int step){ if(step==8){ for(int i=0;i<9;i++) cout<<path[i]; cout<<endl; return ; } for(int i=1;i<6;i++){ if(edge[pos][i]&&visit[pos][i]){ visit[pos][i]=false; visit[i][pos]=false; path[step+1]=i; dfs(i,step+1); visit[pos][i]=true; visit[i][pos]=true; } } } int main(){ edge[1][2]=true;edge[1][5]=true;edge[1][3]=true; edge[2][1]=true;edge[2][5]=true;edge[2][3]=true; edge[3][2]=true;edge[3][5]=true;edge[3][4]=true; edge[3][1]=true; edge[4][3]=true;edge[4][5]=true;edge[5][3]=true; edge[5][4]=true;edge[5][1]=true;edge[5][2]=true; for(int i=0;i<6;i++) for(int j=0;j<6;j++) visit[i][j]=edge[i][j]; path[0]=1; dfs(1,0); return 0; }