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;
    }
    
  • 相关阅读:
    10w行级别数据的Excel导入优化记录
    迷失的人
    spring切面表达式详解
    json-path(json查找与操作)
    sprongboot yml文件语法
    消息队列面试突击系列--消息经典问题解决
    消息队列面试突击系列--消息产品对比
    mysql系列--sql实现原理
    mysql系列--锁和MVCC
    mysql系列--索引
  • 原文地址:https://www.cnblogs.com/AWCXV/p/8177357.html
Copyright © 2011-2022 走看看