2018.06.03

poj 2488

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
int dix[] = {-1,1,-2,2,-2,2,-1,1} ;
int diy[] = {-2,-2,-1,-1,1,1,2,2} ;
const int maxn = 69 ;
int xx[maxn],yy[maxn] ;
int mark,m,n;
int vis[maxn][maxn] ;
void dfs(int x,int y,int step)
{
    int ax,ay ;
    vis[x][y] = step ;
    if(m*n == step)
    {
        mark = 1 ;
        return ;
    }
    for(int i = 0 ; i < 8 ; i++)
    {
        ax = x+dix[i] ;
        ay = y+diy[i] ;
        if(ax>=0&&ax<m&&ay>=0&&ay<n&&!vis[ax][ay])
        {
            xx[step] = ax ;
            yy[step] = ay ;
            dfs(ax,ay,step+1);
            if(mark)
                return;
            vis[ax][ay] = 0 ;
        }
    }
}
void Init()
{
    memset(vis,0,sizeof(vis)) ;
    memset(xx,0,sizeof(xx)) ;
    memset(yy,0,sizeof(yy)) ;
}
int main()
{
    int t;
    cin>>t;
    for(int i = 1 ; i <= t ; i ++)
    {
        cin>>m>>n;
        int step ;
        cout<<"Scenario #"<<i<<":"<<endl;
        Init() ;
        mark = 0 ;
        step = 1 ;
        xx[0] = 0 ;
        yy[0] = 0 ;
        dfs(0,0,step) ;
        if(mark)
        {
            for(int k = 0 ; k < m*n ; k++)
                printf("%c%d",yy[k]+'A',xx[k]+1);
            cout<<endl<<endl;
        }
        if(!mark)
            cout<<"impossible"<<endl<<endl;
    }
    return 0 ;
}

poj 3278

bfs入门题

poj 3009

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=25;
int n,m,ans,sx,sy;
int map[maxn][maxn],movex[4]={0,0,1,-1},movey[4]={1,-1,0,0};
bool isborder(int x,int y)
{
    if(x<=0||x>n||y<=0||y>m)
    return true;
    return false;
}
void DFS(int x,int y,int step)
{
    if(step>10)
    return;
    for(int i=0;i<4;i++)
    {
    int itx=x+movex[i];
    int ity=y+movey[i];
    if(isborder(itx,ity)||map[itx][ity]==1)
        continue;
    while(1)
    {
        if(isborder(itx,ity))
        break;
        if(map[itx][ity]==0||map[itx][ity]==2)
        {
        itx+=movex[i];
        ity+=movey[i];
        continue;
        }
        if(map[itx][ity]==1)
        {
        map[itx][ity]=0;
        DFS(itx-movex[i],ity-movey[i],step+1);
        map[itx][ity]=1;
        break;
        }
        if(map[itx][ity]==3)
        {
        ans=min(ans,step+1);
        return;
        }
    }
    }
}
int main()
{
    while(scanf("%d%d",&m,&n)&&(n+m))
    {
    ans=11;
    for(int i=0;i<=n+1;i++)
        map[i][0]=map[i][m+1]=1;
    for(int i=0;i<=m+1;i++)
        map[0][i]=map[n+1][i]=1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
        scanf("%d",&map[i][j]);
        if(map[i][j]==2)
        {
            sx=i;
            sy=j;
        }
        }
    DFS(sx,sy,0);
    printf("%d\n",ans==11?-1:ans);
    }
    return 0;
}

24点问题

Nightmare

Find a way
两次bfs

猜你喜欢

转载自blog.csdn.net/qq_36616023/article/details/80558039