zoukankan      html  css  js  c++  java
  • 【SCOI 2005】 骑士精神

    【题目链接】

                https://www.lydsy.com/JudgeOnline/problem.php?id=1085

    【算法】

               IDA*

    【代码】

               

    #include<bits/stdc++.h>
    using namespace std;
    const int dx[8] = {-2,-2,-1,-1,1,1,2,2};
    const int dy[8] = {-1,1,-2,2,-2,2,-1,1};
    const int goal[5][5] = 
    {    
    {1,1,1,1,1},
    {0,1,1,1,1},
    {0,0,2,1,1},
    {0,0,0,0,1},
    {0,0,0,0,0} 
    };
    
    int i,j,T,x,y,step;
    char c;
    bool solved;
    int mp[5][5];
    
    inline bool valid(int x,int y)
    {
            return x >= 0 && x < 5 && y >= 0 && y < 5;
    }
    inline int f()
    {
            int i,j;
            int ret = 0;
            for (i = 0; i < 5; i++)
            {
                    for (j = 0; j < 5; j++)
                    {
                            if (mp[i][j] != goal[i][j])
                                    ret++;        
                    }    
            }        
            return ret - 1;
    }
    
    inline bool IDDFS(int dep,int x,int y)
    {
            int i,tx,ty;
            if (dep + f() > step) return false;
            if (dep == step) return true;
            for (i = 0; i < 8; i++)
            {
                    tx = x + dx[i];
                    ty = y + dy[i];
                    if (valid(tx,ty))
                    {
                            swap(mp[x][y],mp[tx][ty]);
                            if (IDDFS(dep+1,tx,ty)) return true;
                            swap(mp[x][y],mp[tx][ty]);
                    }
            }
            return false;
    }
    
    int main() 
    {
            
            scanf("%d",&T);
            getchar();
            while (T--)
            {
                    solved = false;
                    for (i = 0; i < 5; i++)
                    {
                            for (j = 0; j < 5; j++)
                            {
                                    c = getchar();
                                    if (c == '0')    mp[i][j] = 0;
                                    else if (c == '1') mp[i][j] = 1;
                                    else 
                                    {
                                            x = i;
                                            y = j;
                                            mp[i][j] = 2;    
                                    }
                            }    
                            getchar();
                    }    
                    for (i = 0; i <= 15; i++)
                    {
                            step = i;
                            if (IDDFS(0,x,y))    
                            {
                                    printf("%d
    ",i);
                                    solved = true;
                                    break;
                            }
                    }    
                    if (!solved) printf("-1
    ");
            }
            
            return 0;
        
    }
  • 相关阅读:
    3.1C#中的命名空间
    2章总结
    2.4冒泡排序
    2.3 C#中的数组
    2.2二重循环
    2.1c#中的循环语句
    1章总结
    docker内外数据拷贝
    搭建docker环境
    centos7 部署Apache的httpd服务器
  • 原文地址:https://www.cnblogs.com/evenbao/p/9275392.html
Copyright © 2011-2022 走看看