zoukankan      html  css  js  c++  java
  • poj2488骑士马走

    #include<stdio.h>
    #include<stdlib.h>
    int data[100][100] = {0};
    int Dx[8] = {-1,1,-2,2,-2,2,-1,1};
    int Dy[8] = {-2,-2,-1,-1,1,1,2,2};
    int row,col;
    int cun[100][2] = {0};
    bool ret = false;
    void DFS(int x,int y,int step);
    int main()
    {
    	int n;
    	//freopen("a.txt","r",stdin);
    	scanf("%d",&n);
    	for(int l = 0;l < n;l++)
    	{
    		ret = false;
    		scanf("%d",&row);
    		scanf("%d",&col);
    		printf("Scenario #%d:
    ",l + 1);
    		
    		data[0][0] = 1;
    		DFS(0,0,1);
    		data[0][0] = 0;
    		if(ret){
    			goto v;
    		}
    		
    		if(ret == false){
    			printf("impossible
    
    ");
    			continue;
    		}
    		v:for(int k = 0;k < row * col;k++)
    		  {
    				printf("%c%d",cun[k][1] + 65,cun[k][0] + 1);
    				cun[k][0] = 0;
    				cun[k][1] = 0;
    		  }
    		  printf("
    
    ");
    	}
    	return 0;
    }
    void DFS(int x,int y,int step)
    {
    	if(step == row * col)
    	{
    		ret = true;
    		return;
    	}
    	for(int i = 0;i < 8;i++)
    	{
    		int Nx = x + Dx[i];
    		int Ny = y + Dy[i];
    		if(data[Nx][Ny] == 0 && Nx >= 0 && Nx < row && Ny >= 0 && Ny < col)
    		{
    			data[Nx][Ny] = 1;
    			cun[step][0] = Nx;
    			cun[step][1] = Ny;
    			DFS(Nx,Ny,step + 1);
    			data[Nx][Ny] = 0;
    			if(ret)
    			{
    				return;
    			}
    			cun[step][0] = 0;
    			cun[step][1] = 0;
    			
    		}
    	}
    }
    
  • 相关阅读:
    20170419数据结构
    20170418 random函数和range函数
    20170418 sum函数、
    20170417嵌套循环
    20170417循环(loop)
    linux 输入输出重定向
    cut 命令-截取文件中指定内容
    read 命令-从键盘读取变量的值
    xargs-命令
    find 在目录中查找文件
  • 原文地址:https://www.cnblogs.com/452035305qq/p/6252220.html
Copyright © 2011-2022 走看看