zoukankan      html  css  js  c++  java
  • P2324 [SCOI2005]骑士精神(迭代加深+dfs)

    迭代加深

    • 在限定的步数内,进行最优化剪枝。
    • 一般题给都会给出限定条件。

    P2324 [SCOI2005]骑士精神

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define DOF 0x7f7f7f7f
    #define endl '
    '
    #define mem(a, b) memset(a, b, sizeof(a))
    #define debug(case, x) cout << case << "  : " << x << endl
    #define open freopen("ii.txt", "r", stdin)
    #define close freopen("oo.txt", "w", stdout)
    #define IO                       
        ios::sync_with_stdio(false); 
        cin.tie(0);                  
        cout.tie(0)
    #define pb push_back
    using namespace std;
    //#define int long long
    #define lson rt << 1
    #define rson rt << 1 | 1
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<long long, long long> PII;
    const int maxn = 1e6 + 10;
    
    
    int a[7][7], t[7][7] = {
        {0},
        {0, 1, 1, 1, 1, 1},
        {0, 0, 1, 1, 1, 1},
        {0, 0, 0, -1, 1, 1},
        {0, 0, 0, 0, 0, 1},
        {0, 0, 0, 0, 0, 0}
    };
    int d = 0, dir[8][2] = {
        {1, 2}, {1, -2}, {-1, 2}, {-1, -2},
        {2, 1}, {2, -1}, {-2, 1}, {-2, -1}
    };
    
    
    int gtg(){
        int res=0;
        for(int i=1;i<=5;++i){
            for(int j=1;j<=5;++j){
                if(a[i][j]!=t[i][j])++res;
            }
        }
        return res;
    }
    
    bool dfs(int x,int y,int dep){
        int tmp=gtg();
        if(tmp+dep>d+1) return false;
        if(!tmp)return true;
        for(int i=0;i<8;++i){
            int xx=x+dir[i][0],yy=y+dir[i][1];
            if(x>=1&&x<=5&&y>=1&&y<=5){
                swap(a[x][y],a[xx][yy]);
                if(dfs(xx,yy,dep+1)) return true;
                swap(a[x][y],a[xx][yy]);
            }
        }
        return false;
    }
    
    void solve() {
        int x, y;
        for(int i = 1; i <= 5; ++i) {
            string str;
            cin >> str;
            for(int j = 1; j <= 5; ++j) {
                if(str[j - 1] == '*')a[i][j] = -1, x = i, y = j;
                else a[i][j]=str[j-1]-'0';
            }
        }
    
        for(d = 0; d<=15;++d){
            if(dfs(x,y,0)){
                cout<<d<<endl;
                return ;
            }
        }
        cout<<-1<<endl;
    
    }
    
    
    int main() {
        int t;
        cin >> t;
        while(t--) {
            solve();
        }
    
    }
    
  • 相关阅读:
    c#添加托盘图标右键后显示菜单选项
    mysql workbench修改字段报错
    mysql增删改查语句
    连接win7的mysql很慢解决方法
    uvm_cmdline_processor
    cadence help文件库调出指令 :cdnshelp
    设置开机自动运行vncserver
    Vector bit-select and part-select addressing verilog片选写法
    Verilog三段式状态机描述
    同步FIFO学习
  • 原文地址:https://www.cnblogs.com/waryan/p/13451809.html
Copyright © 2011-2022 走看看