许久没有看东西,上午看到的经典的跳马题,有所感触,这些东西真的要经常的看看额,真的生的像没学过一样额,趁着这道题,又去看了一道广搜的题,把这两种方法的大体的框架回顾了一下。跳马 经典广搜(跳马)
对深搜还是要理解回溯的问题。
Description
深搜经典跳马
The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey
around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?
Problem
Find a path such that the knight visits every square once. The knight can start and end on any square of the board.
Input
Output
If no such path exist, you should output impossible on a single line.
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4
#if 1
#include<iostream>
#include<string.h>
using namespace std;
int dx[8][2]{{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}},p,q,ax[650],ay[650];
bool flag,vis[50][50];
int OK(int x,int y)
{
if(x>=0 && x<p && y>=0 && y<q)
return 1;
else
return 0;
}
void dfs(int x,int y,int t)
{
ax[t]=x;
ay[t]=y;
vis[x][y]=1;
if(t==p*q-1)
{
flag=1;
return;
}
for(int i=0; i<8; i++)
{
int bx=x+dx[i][0];
int by=y+dx[i][1];
if(OK(bx,by) && !vis[bx][by])
{
vis[bx][by]=1;
dfs(bx,by,t+1);
if(flag)
return;
vis[bx][by]=0;
}
}
}
int main()
{
int x,ans=0;
cin>>x;
while(x--)
{
memset(ax,0,sizeof(ax));
memset(ay,0,sizeof(ay));
memset(vis,false,sizeof(vis));
flag=0;
cin>>p>>q;
for(int i=0; i<p; i++)
{
for(int j=0; j<q; j++)
{
dfs(i,j,0);
if(flag)
{
break;
}
}
if(flag)
{
break;
}
}
if(flag)
{
cout<<"Scenario #"<<++ans<<endl;
for(int i=0; i<p*q; i++)
cout<<char(ay[i]+'A')<<ax[i]+1;
cout<<endl<<endl;
}
else
cout<<"impossible"<<endl<<endl;
}
}
#endif
回顾的广搜跳马问题:
knight moves经典广搜
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy. |
Sample Output
|
#if 1
#include<bits/stdc++.h>
using namespace std;
int step[8][8];
int dir[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
bool pd(int x,int y)
{
if(x>=0 && x<=7 && y>=0 && y<=7)
return 1;
else return 0;
}
int starx,stary,endx,endy;
int bfs()
{
memset(step,0,sizeof(step));
queue <int>q;
int x,y,x1,y1;
q.push(starx);
q.push(stary);
step[x][y]=0;
while(!q.empty())
{
x=q.front();
q.pop();
y=q.front();
q.pop();
if(x==endx && y==endy) //看看现在的位置是不是
return step[x][y];
else
{
for(int i=0; i<=7; i++)
{
x1=x+dir[i][0];
y1=y+dir[i][1];
if(pd(x1,y1))
{
q.push(x1);
q.push(y1);
step[x1][y1]=step[x][y]+1;
}
}
}
}
}
int main()
{
char a[3],b[3];
while(cin>>a>>b)
{
starx=a[0]-'a';
stary=a[1]-'1';
endx=b[0]-'a';
endy=b[1]-'1';
int ans=bfs();
cout<<"To get from "<<a<<" to "<<b<<" takes "<<ans<<" knight moves."<<endl;
}
}
#endif
这俩题让自己回忆一下深搜和广搜的框架。搜索主要的写的主要还是这两个题,最后看了看课本,放假的dp基本没复习,杭电题没开的时候,重点看了看书上的dp题。