Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

顺序果然很重要,922MS,求非递归解法,附代码

Posted by speedcell4 at 2011-07-28 10:05:37 on Problem 2488
#include<iostream>
#include<string>
#define SIZE 30
using namespace std;
int n,m;
int tcase,times=1;
int go[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
bool hash[SIZE][SIZE]={false};
void Init(void)
{
    memset(hash,false,sizeof(hash));
    hash[0][0]=true;
}
bool canGo(int x,int y)
{
    return (x>=0&&y>=0)&&(x<n&&y<m)&&(hash[x][y]==false);
}
bool DFS(int x,int y,int tot,string ans)
{
    if(tot==n*m)
    {
        cout<<ans<<endl<<endl;
        return true;
    }
    else
    {
        for(int i=0;8-i>0;i++)
        {
            int x1=x+go[i][0];
            int y1=y+go[i][1];
            char ans1=y1+'A';
            char ans2=x1+'1';
            if(canGo(x1,y1))
            {
                hash[x1][y1]=true;
                if(DFS(x1,y1,tot+1,ans+ans1+ans2)) return true;
                hash[x1][y1]=false;
            }
        }
        return false;
    }
}            
int main()
{
    for(scanf("%d",&tcase);tcase-times>=0;times++)
    {
        Init();
        scanf("%d %d",&n,&m);
        cout<<"Scenario #"<<times<<":"<<endl;
        if(DFS(0,0,1,"A1")==false) cout<<"impossible"<<endl<<endl;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator