usaco 2.1.1 castle

This is my blog.
背景:In a stroke of luck almost beyond imagination, Farmer John was sent a ticket to the Irish Sweepstakes (really a lottery) for his birthday. This ticket turned out to have only the winning number for the lottery! Farmer John won a fabulous castle in the Irish countryside.
Bragging rights being what they are in Wisconsin, Farmer John wished to tell his cows all about the castle. He wanted to know how many rooms it has and how big the largest room was. In fact, he wants to take out a single wall to make an even bigger room.
Your task is to help Farmer John know the exact room count and sizes.
The castle floorplan is divided into M (wide) by N (1 <=M,N<=50) square modules. Each such module can have between zero and four walls. Castles always have walls on their “outer edges” to keep out the wind and rain.

Consider this annotated floorplan of a castle:
usaco2.1.1.1

题目:castle

输入:

The map is stored in the form of numbers, one number for each module, M numbers on each of N lines to describe the floorplan. The input order corresponds to the numbering in the example diagram above.
Each module number tells how many of the four walls exist and is the sum of up to four integers:

1: wall to the west
2: wall to the north
4: wall to the east
8: wall to the south


Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1.

Line 1: Two space-separated integers: M and N
Line 2..: M x N integers, several per line.

输出:The output contains several lines:

usaco2.1.1.2

my AC code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <fstream>
using namespace std;
int m,n,maxn,num;
int map[55][55];
bool vis[55][55];
// first calculate
bool u[16]={1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0};//2
bool l[16]={1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0};//1
bool r[16]={1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0};//4
bool d[16]={1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0};//8
char a[5]={' ','E','S','N','E'};
int b[2505];//record the square's size
bool judge(int i,int j){
if(i>n||j>m||i<=0||j<=0)return false;
return true;
}
void dfs(int i,int j,int flag){
vis[i][j]=true; num++;
if(judge(i-1,j)&&!vis[i-1][j]&&u[map[i][j]])dfs(i-1,j,flag);
if(judge(i,j-1)&&!vis[i][j-1]&&l[map[i][j]])dfs(i,j-1,flag);
if(judge(i,j+1)&&!vis[i][j+1]&&r[map[i][j]])dfs(i,j+1,flag);
if(judge(i+1,j)&&!vis[i+1][j]&&d[map[i][j]])dfs(i+1,j,flag);
map[i][j]=flag;//can't change before.
}
int main(){
//freopen("castle.out","w",stdout);
//freopen("castle.in","r",stdin);
scanf("%d %d",&m,&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&map[i][j]);
}
}
memset(vis, 0, sizeof(vis));
int flag=0;maxn=-0x7f7f;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!vis[i][j]){
num=0;
dfs(i,j,++flag);
b[flag]=num;
maxn=max(maxn,num);
}
}
}
printf("%d\n",flag);
printf("%d\n",maxn);
int row=0,col=55;char dir;
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++){
// printf("%d ",map[i][j]);
// }
// printf("\n");
// }
for(int i=1;i<=n;i++){
for(int j=m;j>=1;j--){
if(j>1&&map[i][j]!=map[i][j-1]){
int tmp=b[map[i][j]]+b[map[i][j-1]];
if(maxn<tmp||(maxn==tmp&&(col>j-1||(col==j-1&&row<i)))){
maxn=tmp;
row=i;
col=j-1;
dir='E';
//cout<<maxn<<" "<<row<<" "<<col<<" "<<dir<<endl;
}
}
if(i>1&&map[i][j]!=map[i-1][j]){
int tmp=b[map[i][j]]+b[map[i-1][j]];
if(maxn<tmp||(maxn==tmp&&(col>j||(col==j&&row<i)))||(col==j&&row==i&&dir=='E')){
maxn=tmp;
row=i;
col=j;
dir='N';
//cout<<maxn<<" "<<row<<" "<<col<<" "<<dir<<endl;
}
}
}
}
printf("%d\n",maxn);
printf("%d %d %c\n",row,col,dir);
return 0;
}

官网题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
using namespace std;
#define MAXDIM 50
#define MAXN 100
#define MAXCOLOR 100
#define MAXROOM (MAXDIM*MAXDIM)
enum {
Wwest = 1,
Wnorth = 2,
Weast = 4,
Wsouth = 8
};
typedef struct Square Square;
struct Square {
int wall;
int numbered;
int room;
};
int wid, ht;
Square castle[MAXDIM][MAXDIM];
int roomsize[MAXROOM];
void number(int room, int x, int y)
{
int w;
if(castle[x][y].numbered) {
assert(castle[x][y].room == room);
return;
}
castle[x][y].numbered = 1;
castle[x][y].room = room;
roomsize[room]++;
w = castle[x][y].wall;
if(x > 0 && !(w & Wwest))
number(room, x-1, y);
if(x+1 < wid && !(w & Weast))
number(room, x+1, y);
if(y > 0 && !(w & Wnorth))
number(room, x, y-1);
if(y+1 < ht && !(w & Wsouth))
number(room, x, y+1);
}
int main(void)
{
int x, y, w, nroom, bigroom;
int i, n, m, mx, my;
char mc;
scanf("%d %d", &wid, &ht);
/* read in wall info */
for(y=0; y<ht; y++) {
for(x=0; x<wid; x++) {
scanf("%d", &w);
castle[x][y].wall = w;
}
}
/* number rooms */
nroom = 0;
for(y=0; y<ht; y++)
for(x=0; x<wid; x++)
if(!castle[x][y].numbered)
number(nroom++, x, y);
/* find biggest room */
bigroom = roomsize[0];
for(i=1; i<nroom; i++)
if(bigroom < roomsize[i])
bigroom = roomsize[i];
/* look at best that can come of removing an east or north wall */
/*
tips: change the position,so that lessen the condition
normal:
0 0 1 1 2 2 2
0 0 0 1 2 3 2
0 0 0 4 2 4 2
0 4 4 4 4 4 2
now:
0 0 0 0
0 0 0 4
1 0 0 4
1 1 4 4
2 2 2 4
2 3 4 4
2 2 2 2
now choose the right up
actually choose the left down
if m==n will not change ,because the left down is choosen firstly.
and before E,choose the N first.
*/
m = 0;
for(int i=0;i<wid;i++){
for(int j=0;j<ht;j++){
cout<<castle[i][j].room<<" ";
}
cout<<endl;
}
for(x=0; x<wid; x++)
for(y=ht-1; y>=0; y--) {
if(y > 0 && castle[x][y].room != castle[x][y-1].room) {
n = roomsize[castle[x][y].room] + roomsize[castle[x][y-1].room];
if(n > m) {
m = n;
mx = x;
my = y;
mc = 'N';
}
}
if(x+1 < wid && castle[x][y].room != castle[x+1][y].room) {
n = roomsize[castle[x][y].room] + roomsize[castle[x+1][y].room];
if(n > m) {
m = n;
mx = x;
my = y;
mc = 'E';
}
}
}
printf("%d\n", nroom);
printf("%d\n", bigroom);
printf("%d\n", m);
printf("%d %d %c\n", my+1, mx+1, mc);
return 0;
}

官网题解中对最后一句话的条件处理的很好,最后一句话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 the location of the wall with respect to the module.,我理解了一个晚上,首先是farthest应翻译为最大程度上地,而不是最远地,其次不要漏条件,一个是要左下角,第二个是N比E更好,不可遗漏。
官网的代码还有一个优点(目前发现的),它发挥了c++的优点,面向对象,非常清晰。
一道简单dfs题,也学到了许多呢。
转载请注明出处,谢谢。

愿 我是你的小太阳


买糖果去喽