zoukankan      html  css  js  c++  java
  • 【习题 7-10 Uva11214】Guarding the Chessboard

    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    迭代加深搜索。 可以想见最后深度不会很深吧。。 然后皇后之间互相攻击到是允许的。。 就这样

    【代码】

    /*
      	1.Shoud it use long long ?
      	2.Have you ever test several sample(at least therr) yourself?
      	3.Can you promise that the solution is right? At least,the main ideal
      	4.use the puts("") or putchar() or printf and such things?
      	5.init the used array or any value?
      	6.use error MAX_VALUE?
      	7.use scanf instead of cin/cout?
      	8.whatch out the detail input require
    */
    /*
        一定在这里写完思路再敲代码!!!
    */
    #include <bits/stdc++.h>
    using namespace std;
    
    const int O = 50;
    const int N = 100,M = 10;
    
    int n,m;
    int visx[N],visy[N],visc[M+10],visr[M+10];
    char s[M+10][M+10];
    
    int maxnum;
    
    bool ok(){
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= m;j++)
                if (s[i][j]=='X' && !(visr[i] || visc[j]||visx[O+i+j]                                      || visy[O+i-j]))
                        return false;
        return true;
    }
    
    bool dfs(int num,int r,int c){
        if (num==maxnum){
            if (ok()) return true;
            return false;
        }
    
        if (c==m+1) return dfs(num,r+1,1);
    
        if (r>n) return false;
    
        if (dfs(num,r,c+1)) return true;
    
        visc[c]++;visr[r]++;
        visx[r+c+O]++; visy[r-c+O]++;
    
        if (dfs(num+1,r,c+1)) return true;
    
        visc[c]--;visr[r]--;
        visx[r+c+O]--; visy[r-c+O]--;
    
        return false;
    }
    
    int main(){
    	#ifdef LOCAL_DEFINE
    	    freopen("rush_in.txt", "r", stdin);
    	#endif
    	ios::sync_with_stdio(0),cin.tie(0);
    	int kase = 0;
        while (cin >>n >> m && n &&m){
            memset(visx,0,sizeof visx);
            memset(visy,0,sizeof visy);
            memset(visr,0,sizeof visr);
            memset(visc,0,sizeof visc);
            for (int i = 1;i <= n;i++)
                cin >> (s[i]+1);
            for (maxnum = 0; ;maxnum++){
                if (dfs(0,1,1)) break;
            }
            cout <<"Case "<<++kase<<": "<<maxnum<<endl;
        }
    	return 0;
    }
    
  • 相关阅读:
    HDU 2157 How many ways?【矩阵快速幂】
    CodeForces 3 D.Least Cost Bracket Sequence【贪心+优先队列】
    【差分】Tallest Cow
    P2220 [HAOI2012]容易题【快速幂】
    无题II HDU
    PHP编译常见错误
    MySQL编译安装
    Tomcat 单(多)实例部署使用
    lvs+keepalived 高可用及负载均衡
    MySQL操作命令梳理(2)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/8177357.html
Copyright © 2011-2022 走看看