problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4464
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool godie(const int, const int);
bool rg_kill(const int, const int);
bool hkill(const int, const int);
bool ckill(const int, const int);
char map[11][10];
char answer[50][4];
int bx, by, ans = 0;
int main()
{
int n, x, y;
char type;
while(scanf("%d%d%d",&n,&by,&bx) == 3 && n > 1 && n < 8)
{
memset(map,0,sizeof(map));
getchar();
for(int i = 0; i < n; i++) {
scanf("%c%d%d",&type,&y,&x);
getchar();
map[y][x] = type;
}
if((bx > 4 && godie(by,bx-1) || bx == 4) &&
(bx < 6 && godie(by,bx+1) || bx == 6) &&
(by < 3 && godie(by+1,bx) || by == 3) &&
(by > 1 && godie(by-1,bx) || by == 1)) memcpy(answer[ans++],"YES",3);
else memcpy(answer[ans++],"NO",2);
}
for(int i = 0; i < ans; i++) printf("%s\n",answer[i]);
return 0;
}
bool godie(const int by, const int bx)
{
if(!rg_kill(by, bx) && !hkill(by, bx) && !ckill(by, bx)) return false;
return true;
}
bool rg_kill(const int by, const int bx)
{
bool flag = 0;
for(int i = bx+1; i < 10; i++)
if(map[by][i]) {
if(map[by][i] == 'R') flag = 1;
break;
}
for(int i = bx-1; i > 0 && !flag; i--)
if(map[by][i]) {
if(map[by][i] == 'R') flag = 1;
break;
}
for(int i = by+1; i < 11 && !flag; i++)
if(map[i][bx]) {
if(map[i][bx] == 'R' ||
map[i][bx] == 'G') flag = 1;;
break;
}
for(int i = by-1; i > 0 && !flag; i--)
if(map[i][bx]) {
if(map[i][bx] == 'R') flag = 1;
break;
}
return flag;
}
bool hkill(const int by, const int bx)
{
bool flag = 0;
//up
if((map[by+2][bx+1] == 'H' && !map[by+1][bx+1]) ||
(map[by+2][bx-1] == 'H' && !map[by+1][bx-1])) flag = 1;
//right
else if ((by > 1 && map[by-1][bx-2] == 'H' && !map[by-1][bx-1]) ||
(map[by+1][bx-2] == 'H' && !map[by+1][bx-1])) flag = 1;
//down
else if (by > 2 && ((map[by-2][bx+1] == 'H' && !map[by-1][bx+1]) ||
(map[by-2][bx-1] == 'H' && !map[by-1][bx-1]))) flag = 1;
//left
else if ((map[by+1][bx+2] == 'H' && !map[by+1][bx+1]) ||
(by > 1 && map[by-1][bx+2] == 'H' && !map[by-1][bx+1])) flag = 1;
return flag;
}
bool ckill(const int by, const int bx)
{
bool flag = 0, barrier = 0;
int times = 0;
if(by == 3 && map[by-1][bx] && map[by-2][bx] == 'C') flag = 1;
else {
for(int i = bx-1; i != 0 && times < 2; i--)
if(map[by][i]) {
times++;
if(map[by][i] == 'C' && times == 2) {flag = 1; break;}
}
for(int i = bx+1, barrier = 0, times = 0; i < 10 && !flag && times < 2; i++)
if(map[by][i]) {
times++;
if(map[by][i] == 'C' && times == 2) {flag = 1; break;}
}
for(int i = by+1, barrier = 0, times = 0; i < 11 && !flag && times < 2; i++)
if(map[i][bx]) {
times++;
if(map[i][bx] == 'C' && times == 2) {flag = 1; break;}
}
}
return flag;
}