forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgalou.cpp
138 lines (138 loc) · 1.77 KB
/
galou.cpp
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
// 2008-06-20
#include <cstdio>
#ifdef _MSC_VER
#define GC getchar
#define PC putchar
#else
#define GC getchar_unlocked
#define PC putchar_unlocked
#endif
int dx[6]={1, 1, 0,-1,-1,0};
int dy[6]={0,-1,-1, 0, 1,1};
void fix()
{
char c;
do
{
c=GC();
if (c==-1)
return;
}
while (c==' '||c=='\n');
ungetc(c,stdin);
}
int input()
{
fix();
int x=0;
char c;
for(;;)
{
c=GC();
if (c==' '||c=='\n')
return x;
x=10*x+c-'0';
}
}
bool jam;
char a[100][100];
int X,Y;
void dfs(int x,int y,char c)
{
int i,x2,y2;
if (c=='(')
{
if (a[x][y]==')')
{
jam=true;
return;
}
else if (a[x][y]=='(')
return;
else
{
a[x][y]='(';
for (i=0; i<6; i++)
{
x2=x+dx[i]; y2=y+dy[i];
if (x2>=0&&x2<X&&y2>=0&&y2<Y&&a[x2][y2]!='.')
{
dfs(x2,y2,')');
if (jam)
return;
}
}
}
}
else
{
if (a[x][y]=='('||a[x][y]=='I')
{
jam=true;
return;
}
else if (a[x][y]==')')
return;
else
{
a[x][y]=')';
for (i=0; i<6; i++)
{
x2=x+dx[i]; y2=y+dy[i];
if (x2>=0&&x2<X&&y2>=0&&y2<Y&&a[x2][y2]!='.')
{
dfs(x2,y2,'(');
if (jam)
return;
}
}
}
}
}
void dfsjam(int x,int y)
{
a[x][y]='B';
int i,x2,y2;
for (i=0; i<6; i++)
{
x2=x+dx[i]; y2=y+dy[i];
if (x2>=0&&x2<X&&y2>=0&&y2<Y&&a[x2][y2]!='.'&&a[x2][y2]!='B')
dfsjam(x2,y2);
}
}
int main()
{
int i,j;
for(;;)
{
Y=input();
X=input();
if (X==0)
return 0;
PC('\n');
for (j=0; j<Y; j++)
{
for (i=0; i<X; i++)
a[i][j]=GC();
GC();
}
for (i=0; i<X; i++)
for (j=0; j<Y; j++)
if (a[i][j]=='I')
{
jam=false;
dfs(i,j,'(');
if (jam)
dfsjam(i,j);
}
for (j=0; j<Y; j++)
{
for (i=0; i<X; i++)
if (a[i][j]=='*')
PC('F');
else
PC(a[i][j]);
PC('\n');
}
}
}