zoukankan      html  css  js  c++  java
  • [USACO][暴力]The Castle

    题意:

    给一个数字矩阵代表一个有很多墙壁的屋子,墙壁的包围可以形成一个个房间,输出最大的房间大小、最大房间位置以及房间数目。

    思路:

    首先M和N容易反,然后就是拆墙找最大房间的时候要先东西找,再南北找。东西中更西的优先,南北的更南的优先。把这里的东西南北映射成i++,i--,j++,j--这样子。

    再就是color数组做标号的时候,如果roomNumber从0开始的话,color初始化就要是-1,然后搜索搜到有-1就不找他了。我这里是roomNumber从1开始。同时搜索的时候也要注意判断要不要搜这个点的时候,最好先判断再展开搜索。如果在push的时候判断的话会重复。

    最后吐槽一下codeblocks貌似单个文件编译不支持debug,只能靠输出debug,就很容易懒得debug。

    设计的话我把房间二维坐标用Room包装了一下方便存取,color数组负责给房间标号染色。深搜的时候迭代的同时也在更新最大面积,记录当前的房间号的面积。

    代码:

      1 #include<stdio.h>
      2 #include<bits/stdc++.h>
      3 using namespace std;
      4 
      5 struct Room
      6 {
      7     int r,c;
      8     Room(int rr, int cc):r(rr),c(cc){};
      9 };
     10 
     11 int M,N;
     12 int roomNum = 1;
     13 int roomArea;
     14 int maxArea;
     15 int room[300][300];
     16 int color[300][300];
     17 int area[300];
     18 void dfs(int r,int c)
     19 {
     20     stack<Room> stk;
     21     stk.push(Room(r,c));
     22     while(!stk.empty())
     23     {
     24         int i=stk.top().r;
     25         int k=stk.top().c;
     26         stk.pop();
     27         //cout<<i<<" "<<k<<" num: "<<roomNum<<endl;
     28         //编号
     29         if(color[i][k] != 0)
     30             continue;
     31         ++roomArea;
     32         color[i][k]=roomNum;
     33         if((room[i][k] & 1) == 0){
     34             stk.push(Room(i,k - 1));
     35         }
     36         if((room[i][k] & 2) == 0 ){
     37             stk.push(Room(i - 1, k));
     38         }
     39         if((room[i][k] & 4) == 0){
     40             stk.push(Room(i,k + 1));
     41         }
     42         if((room[i][k] & 8) == 0 ){
     43             stk.push(Room(i + 1, k));
     44         }
     45     }
     46     area[roomNum] = roomArea;
     47     if(maxArea < roomArea)
     48     {
     49         maxArea = roomArea;
     50     }
     51     roomArea = 0;
     52     roomNum ++;
     53 }
     54 int main()
     55 {
     56     freopen("castle.in","r",stdin);
     57     freopen("castle.out","w",stdout);
     58     scanf("%d%d",&M,&N);
     59     for(int i =0 ;i < N;i ++)
     60     {
     61         for(int j =0 ;j < M;j ++)
     62         {
     63             int x;
     64             scanf("%d",&room[i][j]);
     65         }
     66     }
     67     for(int i =0 ;i < N;i ++)
     68     {
     69         for(int j =0 ;j < M;j ++)
     70         {
     71             if(color[i][j] == 0)
     72                 dfs(i,j);
     73         }
     74     }
     75     cout<<roomNum - 1<<endl;
     76     cout<<maxArea<<endl;
     77     int ansi = N-1;
     78     int ansj = 0;
     79     char ansch;
     80     int changedMaxArea = area[color[ansi][ansj]];
     81     //j是东西向!farthest to the west优先!!
     82     for(int j = 0;j< M;j ++)
     83     {
     84         for(int i = N - 1;i>= 0;i --)
     85         {
     86             if(i-1>=0 && color[i][j] != color[i - 1][j])
     87             {
     88                 if(area[color[i][j]]+area[color[i - 1][j]] > changedMaxArea)
     89                 {
     90                     changedMaxArea = area[color[i][j]]+area[color[i - 1][j]];
     91                     ansi = i;
     92                     ansj = j;
     93                     ansch = 'N';
     94                 }
     95             }
     96             if(j+1<M && color[i][j] != color[i][j + 1])
     97             {
     98                 if(area[color[i][j]]+area[color[i][j+1]] > changedMaxArea)
     99                 {
    100                     changedMaxArea = area[color[i][j]]+area[color[i][j+1]];
    101                     ansi = i;
    102                     ansj = j;
    103                     ansch = 'E';
    104                 }
    105             }
    106         }
    107     }
    108     cout<<changedMaxArea<<endl;
    109     cout<<ansi+1<<" "<<ansj+1<<" "<<ansch<<endl;
    110 }
    View Code
  • 相关阅读:
    移动SI[转]
    什么是云计算
    ToString()格式和用法大全[转]
    SQLJOIN之完全用法[转]
    DIV样式汇总
    CSS+DIV之强化background属性
    SQLserver中用convert函数转换日期格式
    session丢失与解决办法的资料
    div滤镜结合ajax,实现登录
    什么是ajax
  • 原文地址:https://www.cnblogs.com/ggy778/p/12231337.html
Copyright © 2011-2022 走看看