zoukankan      html  css  js  c++  java
  • BZOJ-1085 骑士精神

    估价函数其实就是与目标状态有几个不同。。。

    迭代启发搜索。

    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <fstream>
    #include <iostream>
    #include <queue>
    
    #define rep(i, l, r) for(int i = l; i <= r; i++)
    #define down(i, l, r) for(int i = l; i >= r; i--)
    #define maxn 123
    #define MAX 1<<30;
    
    using namespace std;
    
    int n[6][6], k, x, y;
    char s[9];
    
    int F()
    {
    	int c = 0;
    	rep(i, 1, 5) if (n[1][i] == 0) c++; 
    	rep(i, 2, 5) if (n[2][i] == 0) c++; 
    	rep(i, 4, 5) if (n[3][i] == 0) c++; 
    	if (n[4][5] == 0) c++; 
    	rep(i, 1, 5) if (n[5][i] == 1) c++; 
    	rep(i, 1, 4) if (n[4][i] == 1) c++; 
    	rep(i, 1, 3) if (n[3][i] == 1) c++; 
    	if (n[2][1] == 1) c++; 
    	return c;
    }
    
    bool Search(int x, int y, int c)
    {
    	if (!c && x == 3 && y == 3 && !F()) return true;
    	if (c < F()-1) return false;
    	//int now = F(), a;
    	int a;
    	if (x > 2 && y > 1) 
    	{ 
    		a = n[x-2][y-1]; n[x-2][y-1] = n[x][y]; n[x][y] = a; 
    		if (Search(x-2, y-1, c-1)) return true; 
    		a = n[x-2][y-1]; n[x-2][y-1] = n[x][y]; n[x][y] = a; 
    	}
    	if (x > 1 && y > 2) 
    	{ 
    		a = n[x-1][y-2]; n[x-1][y-2] = n[x][y]; n[x][y] = a; 
    		if (Search(x-1, y-2, c-1)) return true; 
    		a = n[x-1][y-2]; n[x-1][y-2] = n[x][y]; n[x][y] = a; 
    	}
    	if (x < 5 && y > 2) 
    	{ 
    		a = n[x+1][y-2]; n[x+1][y-2] = n[x][y]; n[x][y] = a; 
    		if (Search(x+1, y-2, c-1)) return true; 
    		a = n[x+1][y-2]; n[x+1][y-2] = n[x][y]; n[x][y] = a; 
    	}
    	if (x < 4 && y > 1) 
    	{ 
    		a = n[x+2][y-1]; n[x+2][y-1] = n[x][y]; n[x][y] = a; 
    		if (Search(x+2, y-1, c-1)) return true; 
    		a = n[x+2][y-1]; n[x+2][y-1] = n[x][y]; n[x][y] = a; 
    	}
    	if (x < 4 && y < 5) 
    	{ 
    		a = n[x+2][y+1]; n[x+2][y+1] = n[x][y]; n[x][y] = a; 
    		if (Search(x+2, y+1, c-1)) return true; 
    		a = n[x+2][y+1]; n[x+2][y+1] = n[x][y]; n[x][y] = a; 
    	}
    	if (x < 5 && y < 4) 
    	{ 
    		a = n[x+1][y+2]; n[x+1][y+2] = n[x][y]; n[x][y] = a; 
    		if (Search(x+1, y+2, c-1)) return true; 
    		a = n[x+1][y+2]; n[x+1][y+2] = n[x][y]; n[x][y] = a; 
    	}
    	if (x > 2 && y < 5) 
    	{ 
    		a = n[x-2][y+1]; n[x-2][y+1] = n[x][y]; n[x][y] = a; 
    		if (Search(x-2, y+1, c-1)) return true; 
    		a = n[x-2][y+1]; n[x-2][y+1] = n[x][y]; n[x][y] = a; 
    	}
    	if (x > 1 && y < 4) 
    	{ 
    		a = n[x-1][y+2]; n[x-1][y+2] = n[x][y]; n[x][y] = a; 
    		if (Search(x-1, y+2, c-1)) return true; 
    		a = n[x-1][y+2]; n[x-1][y+2] = n[x][y]; n[x][y] = a; 
    	}
    	return false;
    }
    
    int main()
    {
    	int t; scanf("%d", &t);
    	while (t--)
    	{
    		rep(i, 1, 5)
    		{
    			scanf("%s", s);
    			rep(j, 1, 5) 
    				if (s[j-1] == '*') x = i, y = j, n[i][j] = 0; 
    				else if (s[j-1] == '0') n[i][j] = 0; 
    				else n[i][j] = 1;
    		}
    		k = 0;
    		while (k <= 15) if (Search(x, y, k)) break; else k++;
    		if (k == 16) k = -1; printf("%d
    ", k);
    	}
    }
    

      

  • 相关阅读:
    SignalR 持久链接 (该功能为手机设备与后台同个用户id进行实现的,仅用signalR学习参考)
    SQL SERVER 分割符转列
    js时间计算加减
    SQL查询历史执行语句
    MSSQL 多行数据串联字符分割单行
    居于HttpWebRequest的HTTP GET/POST请求
    硬件UDP读数AsynUdpClient
    SQL取分组数据的所有第一条数据
    Python 文件的使用
    Python 数据类型
  • 原文地址:https://www.cnblogs.com/NanoApe/p/4316818.html
Copyright © 2011-2022 走看看