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;
    }
    
  • 相关阅读:
    4、Work-Queue
    一个简单servlet容器
    一个简单的Web服务器
    jersey实现RESTful接口PUT方法JSON数据传递
    Java数据库连接组件C3P0和DBCP
    C/S架构和B/S架构
    一些同样适用于人生的计算机原理
    网络编程初探--使用UDP协议的简易聊天室
    IO练习--按字节截取字符串
    IO包中的其他类总结
  • 原文地址:https://www.cnblogs.com/AWCXV/p/8177357.html
Copyright © 2011-2022 走看看