洛谷P1789MC生存插火把
这道题有一个小坑,就是火把照亮的地方可能不在数组里,注意要把那一块地方去掉,这道题的出题者把范围都告诉我们了,大大滴降低了这道题的难度
下面是我的代码
#include <stdio.h>
int n, m,k ;//一共有n*n个数
int a[100][100] = { 0 };//记录火把的位置
int all=0;
int main() {
scanf("%d %d %d", &n, &m, &k);
for (int i = 2; i <= (m + 1); i++) {
int x, y;//用于存储火把的位置
scanf("%d %d", &x, &y);
a[x - 1][y - 1] = 2;//2表示放了火把
for (int x1 = x - 3; x1 <= (x + 1); x1++) {//横着
while(x1<0) x1++;
if (a[x1][y-1] == 0) a[x1][y-1] = 1;//1代表被照亮
//斜着
if (x1 == (x - 2) && a[x1][y - 2] == 0)a[x1][y - 2] = 1;
if (x1 == (x - 2) && a[x1][y] == 0)a[x1][y] = 1;
if (x1 == x && a[x1][y - 2] == 0)a[x1][y - 2] = 1;
if (x1 == x && a[x1][y] == 0)a[x1][y] = 1;
}
for (int y1 = y - 3; y1 <= (y + 1); y1++) {//竖着
while(y1<0) y1++;
if (a[x - 1][y1] == 0) a[x - 1][y1] = 1;
}
}
for (int i = m + 2; i <= m + k + 1; i++) {
int o, p;//储存萤石的位置
scanf("%d %d", &o, &p);
a[o - 1][p - 1] = 3;//3表示放了萤石
for (int o1 = o - 3; o1 <= (o + 1); o1++) {
while(o1<0)o1++;
for (int p1 = p - 3; p1 <= (p + 1); p1++) {
while(p1<0)p1++;
if (a[o1][p1] == 0) a[o1][p1] = 1;//表示被照亮了
}
}
}
for (int u = 0; u < n; u++) {
for (int j = 0; j < n; j++) {
if (a[j][u] == 0) all++;
}
}
printf("%d", all);
}