Usaco--2.1the Castle

Source: Internet
Author: User

Idea: This topic is difficult to build, at the beginning of the time I want to each room without the face of the wall to find out, and then and his adjacent room with a side of the building, and later found to pass the topic given the conditions of the room without a wall is very difficult; After reference to other people's ideas, we record each room that several sides of the (This is easy to do), then does not show the map, directly through the idea of Dfs tag to find all the connected blocks of this graph (Flood fill algorithm). After the processing is relatively simple, to find out the connecting block can know a total of a few rooms, the largest room is how big; then we try to demolish the walls of the n,e face of each room and see the maximum number of rooms available.
For the topic:
Choose the optimal wall to remove from the set of optimal walls by choosing the module farthest to the west (and then, if Still tied, farthest to the south). If still tied, choose ' N ' before ' E '. Name that wall by naming the module that borders it on either the west or south, along with a direction of N or E giving T He location of the wall with respect to the module.
This passage, at the beginning I was very unable to understand, the latter only know, he means: in the big direction we have to first remove the West and then south, for each small room we should first remove the north and then east. The English is too slag.

The code is as follows:

/*id:15674811lang:c++task:castle*/#include <iostream>#include <cstdio>#include <cstring>#include <fstream>using namespace STD;// South-northeast Westintdx[]={1,0,-1,0};intdy[]={0,1,0,-1};/// Southwest North East///int dx1[]={0,1,-1,0};///int dy1[]={-1,0,0,1};// North Eastintdx1[]={-1,0};intdy1[]={0,1};intvis[ -][ -],n,m;Charname[]={' N ',' E '};typedef struct{BOOLflag[5];} P P p[ -][ -];voidDfsintXintYintFlag) { for(intk=0;k<4; k++) {if(P[x][y].flag[k])Continue;intXX=X+DX[K];intYY=Y+DY[K];if(xx<1|| xx>n| | yy<1|| YY&GT;M)Continue;if(Vis[xx][yy])Continue;         Vis[xx][yy]=flag;     DFS (Xx,yy,flag); }}intMain () {Ofstream fout ("Castle.out"); Ifstream Fin ("Castle.in");//ifstream fin ("lkl.txt");     while(fin>>m>>n) {memset(P,0,sizeof(p));memset(Vis,0,sizeof(VIS)); for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) {intX fin>>x;if(x>=8) p[i][j].flag[0]=true, x-=8;if(x>=4) p[i][j].flag[1]=true, x-=4;if(x>=2) p[i][j].flag[2]=true, x-=2;if(x>=1) p[i][j].flag[3]=true; }intCnt=0; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) {if(Vis[i][j])Continue;                vis[i][j]=++cnt;            DFS (I,J,CNT); }intnum[ the];memset(Num,0,sizeof(num));intmax=0; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) {num[vis[i][j]]++;           Max=max (Max,num[vis[i][j]); } fout<<cnt<<endl<<max<<endl;intRoom=0, Rx,ry,k; for(intx=1; x<=n;x++) for(inty=1; y<=m;y++) { for(intD=0;d <2;d + +) {intXX=X+DX1[D];intYY=Y+DY1[D];if(xx<1|| xx>n| | yy<1|| YY&GT;M)Continue;if(Vis[x][y]==vis[xx][yy])Continue;intTMP=NUM[VIS[X][Y]]+NUM[VIS[XX][YY]];if(room<tmp)                        {rx=x; ry=y;                        room=tmp;                    K=d; }Else if(room==tmp) {if(Y<=ry) {if(rx==x&&ry==y)///The same grid North East can be guaranteed North priority                              Continue; Rx=x;                           Ry=y;                       K=d;        }}}} fout<<room<<endl; fout<<rx<<" "<<ry<<" "<<name[k]<<endl; }return 0;}

Usaco--2.1the Castle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.