zoukankan      html  css  js  c++  java
  • hdu 1372 Knight Moves

    嘻嘻,直接BFS

    水过……

    #include<iostream>
    #include<math.h>
    #include<queue>
    using namespace std;
    int map[9][9],si,sj,ei,ej,mins;
    int dir[8][2]={{2,1},{1,2},{-2,1},{2,-1},{1,-2},{-2,-1},{-1,-2},{-1,2}};
    struct node
    {
    	int x,y,dis;
    	node(int _x=0,int _y=0,int _dis=0):x(_x),y(_y),dis(_dis){};   
       /* friend bool operator<(const node &a,const node & b) 
        {     
            return a.dis>b.dis; 
        }*/
    };
    void bfs()
    {
    	node f;
    	f.x=si;f.y=sj;f.dis=0;
    	queue<node> q;
    	q.push(f);
    	while(!q.empty())
    	{
    		node t=q.front();
    		q.pop();
    		if(t.x==ei&&t.y==ej)
    		{
    			mins=t.dis;
    			return ;
    		}
        	for(int k=0;k<8;k++)
    	   {
    		int i=t.x+dir[k][0];
    		int j=t.y+dir[k][1];
    		 if(i<=8&&i>0&&j<=8&&j>0&&!map[i][j])
    		  {
    			map[i][j]=1;
    			q.push(node(i,j,t.dis+1));
    		   }
    		}
    	}
    }
    int main()
    {
    	char a,b;
    	int c,d;
    	while(cin>>a>>c>>b>>d)
    	{
    		si=c;
    		sj=a-'a'+1;
    		ei=d;
    		ej=b-'a'+1;
    		//cout<<si<<' '<<sj<<' '<<ei<<' '<<ej<<endl;
    		memset(map,0,sizeof(map));
    		map[si][sj]=1;
    		mins=100000;
    		bfs();
    		cout<<"To get from "<<a<<c<<" to "<<b<<d<<" takes "<<mins<<" knight moves."<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    第三章感想
    第二章感想
    第一章感想
    第9章 硬件抽象层:HAL
    第10章 嵌入式linux的调试技术
    第8章 蜂鸣器驱动
    第七章 I/O
    第六章 编写Linux驱动程序
    第五章 S3C6410
    源代码的下载和编译
  • 原文地址:https://www.cnblogs.com/nanke/p/2124202.html
Copyright © 2011-2022 走看看