問題描述
給定一個行
列的網(wǎng)格地圖灸叼,從上到下依次編號為第1行到第
行,從左往右依次編號為第1列到第
列鲸郊。每個格子要么可以通行(用
.
表示)赦肃,要么不可通行(用#
表示)溅蛉。
玩家P現(xiàn)在位于某個可以通行的格子上,小地圖顯示出了玩家所在位置周圍3 * 3 = 9個格子他宛。請寫一個程序船侧,根據(jù)地圖和小地圖找到玩家可能所在的位置。
輸入
第一行包含一個正整數(shù)厅各,表示測試數(shù)據(jù)的組數(shù)镜撩。
每組測試數(shù)據(jù)第一行包含兩個正整數(shù),表示行數(shù)和列數(shù)队塘。
接下來行袁梗,每行包含一個長度為
的字符串宜鸯,依次描述每一行每個格子的情況。字符串僅由
.
和#
構(gòu)成遮怜。
接下來3行淋袖,每行包含一個長度為3的字符串,描述玩家所在位置周圍的情況锯梁。字符串由.
即碗、#
、P
(表示玩家所在位置)和*
(表示超出地圖的部分)構(gòu)成陌凳。輸入數(shù)據(jù)保證有且僅有一個P
剥懒,且P
一定位于小地圖的第2行第2列。
請注意:你不能旋轉(zhuǎn)/翻轉(zhuǎn)小地圖合敦。
輸出
對于每組數(shù)據(jù)初橘,輸出一行一個整數(shù),即玩家可能所在的位置的數(shù)量蛤肌。
樣例輸入
2
5 8
#.#....#
#.#..#..
.#..#.#.
#..###..
...#....
.#.
#P.
...
1 2
.#
***
*P#
***
樣例輸出
2
1
解題思路
硬核模擬壁却。
只要判斷一下邊界就行了,所以比較簡單裸准,直接給帶注釋的代碼即可。
代碼
注釋非常清晰了赔硫,肯定能懂炒俱。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <list>
#include <set>
#include <utility> // pair
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm> // sort
#include <string>
#include <stack>
#include <queue>
#include <fstream>
using namespace std;
#define ll long long
#define uchar unsigned char
#define ushort unsigned short
#define uint unsigned int
#define ulong unsigned long
#define ull unsigned long long
#define pi acos(-1)
#define mx(a,b) (a) > (b) ? (a) : (b)
#define mn(a,b) (a) < (b) ? (a) : (b)
#define mem(a,b) memset(a,b,sizeof(a))
#define fre(a) freopen(a,"r",stdin)
#define itn int
#define nit int
#define inr int
#define mian main
#define ednl endl
#define fro for
#define fir for
#define reutrn return
#define retunr return
char a[110][110];
char p[5][5];
int m,n;
int dx[] = {-1,-1,1,1,1,-1,0,0}; // 8個方向
int dy[] = {-1,0,0,-1,1,1,-1,1}; // 8個方向
int chk(int x,int y)
{
if (a[y][x] == '#') return 0; // 如果玩家在墻上,直接0
int xx,yy; // 玩家四周的坐標(biāo)
for (int i = 0;i < 8;i ++)
{
xx = x + dx[i]; // 玩家周圍坐標(biāo)的x
yy = y + dy[i]; // 玩家周圍坐標(biāo)的y
if (xx >= 0 && xx < m && yy >= 0 && yy < n) // 如果玩家周圍一圈在地圖內(nèi)
{
if (a[yy][xx] != p[1 + dy[i]][1 + dx[i]]) return 0; // 直接判斷和我們要的一不一樣即可
} else {
if (p[1 + dy[i]][1 + dx[i]] != '*') return 0; // 在墻外也判一下
}
}
return 1; // 沒問題當(dāng)然true
}
int main()
{
int T;
scanf("%d",&T);
while (T --)
{
// 各種輸入
scanf("%d %d",&n,&m);
for (int i = 0;i < n;i ++)
{
scanf(" ");
for (int j = 0;j < m;j ++)
{
scanf("%c",&a[i][j]);
}
}
for (int i = 0;i < 3;i ++)
{
scanf(" ");
for (int j = 0;j < 3;j ++)
{
scanf("%c",&p[i][j]);
}
}
// 定義開始判斷的x軸的start和end爪膊,y軸的start和end
int xs = 1,ys = 1,xe = m - 1,ye = n - 1; // 由于沒有*的時候玩家不可能在邊緣出現(xiàn)权悟,所以他們一定是從1(數(shù)組從0開始)開始走,到邊界處-1推盛,注意這邊的end是不包括的峦阁,還是左閉右開區(qū)間。
/* 例如:
* #..#..#
* #....#.
* ###...#
* #......
* 玩家可能的活動范圍只有中間的:
* ...#.
* ##...
* x軸也就是[1,5]
* y軸是[1,2]
*/
// 如果是*耘成,那么就得從邊界開始了
if (p[0][1] == '*') ys --;
if (p[1][0] == '*') xs --;
if (p[2][1] == '*') ye ++;
if (p[1][2] == '*') xe ++;
// 當(dāng)然邊界的話循環(huán)的時候可以只判斷邊界榔昔,由于這題數(shù)據(jù)不是很大,所以后面直接硬核模擬整個也沒問題
int cnt = 0; // 計數(shù)
for (int i = ys;i < ye;i ++)
{
for (int j = xs;j < xe;j ++)
{
if (chk(j, i)) cnt ++; // 檢查
}
}
printf("%d\n",cnt);
}
return 0;
}