zoukankan      html  css  js  c++  java
  • hdu 1045 dfs图的遍历 一维化

    1. #include <iostream>
    2. using namespace std;
    3. char map[10][10];
    4. int n;
    5. int sum;
    6. int judge(int k) //判断该点能否放置大炮
    7. {
    8. int x = k / n;
    9. int y = k % n;
    10. int i;
    11. for(i=y; i>=0; i--) //从该点往左找
    12. {
    13. if(map[x][i] == 'X')
    14. {
    15. break;
    16. }
    17. if(map[x][i] == '@')
    18. {
    19. return false;
    20. }
    21. }
    22. for(i=x; i>=0; i--) //从该点往上找
    23. {
    24. if(map[i][y] == 'X')
    25. {
    26. break;
    27. }
    28. if(map[i][y] == '@')
    29. {
    30. return false;
    31. }
    32. }
    33. return true;
    34. }
    35. void dfs(int k, int cnt)
    36. {
    37. if(k == n * n)
    38. {
    39. if(cnt > sum)
    40. {
    41. sum = cnt;
    42. }
    43. return ;
    44. }
    45. if(map[k/n][k%n] == '.' && judge(k)) //若该点能放置大炮
    46. {
    47. map[k/n][k%n] = '@'; //'@' 标记为大炮
    48. dfs(k+1,cnt+1);
    49. map[k/n][k%n] = '.'; //回溯时恢复该点状态
    50. }
    51. dfs(k+1,cnt); //不放
    52. }
    53. int main()
    54. {
    55. while(cin>>n,n)
    56. {
    57. sum = 0;
    58. for(int i=0; i<n; i++)
    59. {
    60. cin>>map[i];
    61. }
    62. dfs(0,0);
    63. cout<<ans<<endl;
    64. }
    65. return 0;
    66. }
    67. /**********************************************************************************************************
    68. Problem Description
    69. Suppose that we have a square city with straight streets.
    70. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
    71. A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
    72. Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
    73. The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
    74. The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
    75. Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
    76. Input
    77. The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.
    78. Output
    79. For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
    80. Sample Input
    81. 4
    82. .X..
    83. ....
    84. XX..
    85. ....
    86. 2
    87. XX
    88. .X
    89. 3
    90. .X.
    91. X.X
    92. .X.
    93. 3
    94. ...
    95. .XX
    96. .XX
    97. 4
    98. ....
    99. ....
    100. ....
    101. ....
    102. 0
    103. Sample Output
    104. 5
    105. 1
    106. 5
    107. 2
    108. 4
    109. *********************************************************************************************************************************/





    附件列表

    • 相关阅读:
      SIP 中的Dialog,call,session 和 transaction
      mysql主从同步出错故障处理总结[数据库技术]
      This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法
      socket函数sendto与send的区别
      linux UDP并发服务器
      UDP并发服务器
      使用mstest.exe 命令行跑test case(不安装Visual Studio 2010)
      树莓派使用qBittorrent进行挂机下载
      一个方便的用于创建树莓派 SD 卡镜像的程序
      定制自己使用的树莓派操作系统
    • 原文地址:https://www.cnblogs.com/sober-reflection/p/813d571da4fe88398ac22643da368356.html
    Copyright © 2011-2022 走看看