zoukankan      html  css  js  c++  java
  • poj1321棋盘问题

    http://poj.org/problem?id=1321

    由于忘记取消起点那里的标记 WA了一次 有点类似皇后

    View Code
     1 #include <stdio.h>
     2 #include<string.h>
     3 int n, m,count,x[11],y[11];
     4 char c[10][10];
     5 void dfs(int i, int j, int v)
     6 {
     7     int p,q;
     8     if(v == m)
     9     count++;
    10     else
    11     {
    12         for(p = i+1 ; p <= n ; p++)
    13             for(q = 1 ; q <= n ; q++)
    14             {
    15                 if(c[p][q]=='#'&&x[p]==0&&y[q]==0)
    16                 {
    17                     x[p]=y[q] = 1;
    18                     dfs(p,q,v+1);
    19                     x[p]=y[q] = 0;
    20                 }
    21             }
    22     }
    23 }
    24 int main()
    25 {
    26     int i, j,num;
    27     while(scanf("%d%d%*c", &n, &m),n!=-1&&m!=-1)
    28     {
    29         num = 0;
    30         count = 0;
    31         memset(x,0,sizeof(x));
    32         memset(y,0,sizeof(y));
    33         for(i = 1 ; i <= n ; i++)
    34         {
    35             for(j = 1 ; j <= n ; j++)
    36             {
    37                 scanf("%c",&c[i][j]);
    38                 if(c[i][j] == '#')
    39                 num++;
    40             }
    41             getchar();
    42         }
    43         for(i = 1 ; i <= n ; i++)
    44         for(j = 1 ; j <= n ; j++)
    45         {
    46             if(num>=m)
    47             {
    48                 if(c[i][j] == '#')
    49                 {
    50                     x[i] = 1;
    51                     y[j] = 1;
    52                     num--;
    53                     dfs(i,j,1);
    54                     x[i] = 0;
    55                     y[j] = 0;
    56                 }
    57             }
    58             else
    59             break;
    60         }
    61         printf("%d\n",count);
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    日志/异常处理(nnlog+traceback)
    Excel操作
    商品管理系统
    大乐透作业
    随机生成密码作业
    时间相关的模块
    os模块
    sys模块
    Pytho中dict(或对象)与json之间的互相转化
    Python三元表达式和列表生成式
  • 原文地址:https://www.cnblogs.com/shangyu/p/2588440.html
Copyright © 2011-2022 走看看