训练总结7.31

来源:互联网 发布:addiction动作数据mmd 编辑:程序博客网 时间:2024/06/05 23:47

许久没有看东西,上午看到的经典的跳马题,有所感触,这些东西真的要经常的看看额,真的生的像没学过一样额,趁着这道题,又去看了一道广搜的题,把这两种方法的大体的框架回顾了一下。跳马       经典广搜(跳马)

对深搜还是要理解回溯的问题。

Description


深搜经典跳马


Background
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

The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
If no such path exist, you should output impossible on a single line.

Sample Input

31 12 34 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;}elsecout<<"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.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.



Sample Input

e2 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6


 

Sample Output
To get from e2 to e4 takes 2 knight moves.To get from a1 to b2 takes 4 knight moves.To get from b2 to c3 takes 2 knight moves.To get from a1 to h8 takes 6 knight moves.To get from a1 to h7 takes 5 knight moves.To get from h8 to a1 takes 6 knight moves.To get from b1 to c3 takes 1 knight moves.To get from f6 to f6 takes 0 knight moves.

#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题。