红与黑
有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。
你站在其中一块黑色的瓷砖上,只能向相邻(上下左右四个方向)的黑色瓷砖移动。
请写一个程序,计算你总共能够到达多少块黑色的瓷砖。
输入格式
输入包括多个数据集合。
每个数据集合的第一行是两个整数 W 和 H,分别表示 x 方向和 y方向瓷砖的数量。
在接下来的 H 行中,每行包括 W个字符。每个字符表示一块瓷砖的颜色,规则如下
1)‘.’:黑色的瓷砖;
2)‘#’:红色的瓷砖;
3)‘@’:黑色的瓷砖,并且你站在这块瓷砖上。该字符在每个数据集合中唯一出现一次。
当在一行中读入的是两个零时,表示输入结束。
输出格式
对每个数据集合,分别输出一行,显示你从初始位置出发能到达的瓷砖数(记数时包括初始位置的瓷砖)。
数据范围
1≤W,H≤20
输入样例:
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
0 0
输出样例:
45
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 30;
char a[N][N];
bool st[N][N];//状态数组
int cnt,w,h;
PII start;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void dfs(int xx, int yy) {
for (int i = 0; i < 4; i++) {
int x1 = xx + dx[i];
int y1 = yy + dy[i];
if (a[x1][y1] == '#') continue;
if (x1 >= 1 && x1 <= h && y1 >= 1 && y1 <= w&& !st[x1][y1]) {
if (a[x1][y1] == '.') cnt++;
st[x1][y1] = true;
dfs(x1, y1);
}
}
}
void bfs(PII start){
queue<PII> q;
q.push(start);
while(q.size()){
auto u=q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int x1 = u.x + dx[i];
int y1 = u.y + dy[i];
if (a[x1][y1] == '#') continue;
if (x1 >= 1 && x1 <= h && y1 >= 1 && y1 <= w&& !st[x1][y1]) {
if (a[x1][y1] == '.') cnt++;
st[x1][y1] = true;
q.push({x1,y1});
}
}
}
}
int main() {
while (cin >> w >> h, w || h) {
memset(st, false, sizeof(st));
memset(a, '#', sizeof(a));
cnt=0;
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++) {
cin >> a[i][j];
if (a[i][j] == '@') start.x = i, start.y = j;
}
dfs(start.x, start.y);
// bfs(start);
cout << cnt+1 << endl;
}
return 0;
}
//floodfill
全球变暖
你有一张某海域 N×N像素的照片,”.”表示海洋、”#”表示陆地,如下所示:
.......
.##....
.##....
....##.
..####.
...###.
.......
其中”上下左右”四个方向上连在一起的一片陆地组成一座岛屿,例如上图就有 2 座岛屿。
由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。
具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。
例如上图中的海域未来会变成如下样子:
.......
.......
.......
.......
....#..
.......
.......
请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。
输入格式
第一行包含一个整数N。
以下 N行 N 列,包含一个由字符”#”和”.”构成的 N×N 字符矩阵,代表一张海域照片,”#”表示陆地,”.”表示海洋。
照片保证第 1 行、第 1列、第 N 行、第 N 列的像素都是海洋。
输出格式
一个整数表示答案。
数据范围
1≤N≤1000
输入样例1:
7
.......
.##....
.##....
....##.
..####.
...###.
.......
输出样例1:
1
输入样例2:
9
.........
.##.##...
.#####...
.##.##...
.........
.##.#....
.#.###...
.#..#....
.........
输出样例2:
1
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
const int N = 1010;
char a[N][N];
int n, res;
bool st[N][N];
int dx[4] = {-1, 0, 0, 1}, dy[4]={0, 1, -1, 0};
void bfs(PII start) {
int total = 0, bound = 0;
st[start.x][start.y] = true;
queue<PII> q;
q.push(start);
while(q.size()) {
auto t = q.front();
total++;
q.pop();
bool is_bound = false;
for(int i = 0; i < 4; i++) {
int x1 = t.x + dx[i], y1 = t.y + dy[i];
if(x1 < 0 || x1 >= n || y1 < 0 || y1 >= n) continue; // 出界
if(a[x1][y1] == '.') { //与海洋相连
is_bound = true;
continue;
}
if(st[x1][y1]) continue;
q.push({x1, y1});
st[x1][y1] = true;
}
if(is_bound) bound++;
}
if(total == bound) res++;
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%s", &a[i]);
}
PII start;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(!st[i][j] && a[i][j] == '#') {
start = {i, j};
bfs(start);
}
}
}
cout<<res;
return 0;
}
献给阿尔吉侬的花束
阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫。
今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪。
现在研究员们想知道,如果阿尔吉侬足够聪明,它最少需要多少时间就能吃到奶酪。
迷宫用一个 R×C 的字符矩阵来表示。
字符 S 表示阿尔吉侬所在的位置,字符 E 表示奶酪所在的位置,字符 # 表示墙壁,字符 . 表示可以通行。
阿尔吉侬在 1 个单位时间内可以从当前的位置走到它上下左右四个方向上的任意一个位置,但不能走出地图边界。
输入格式
第一行是一个正整数 T,表示一共有 T 组数据。
每一组数据的第一行包含了两个用空格分开的正整数 R 和 C,表示地图是一个 R×C 的矩阵。
接下来的 R 行描述了地图的具体内容,每一行包含了 C 个字符。字符含义如题目描述中所述。保证有且仅有一个 S 和 E。
输出格式
对于每一组数据,输出阿尔吉侬吃到奶酪的最少单位时间。
若阿尔吉侬无法吃到奶酪,则输出“oop!”(只输出引号里面的内容,不输出引号)。
每组数据的输出结果占一行。
数据范围
1<T≤10,
2≤R,C≤200
输入样例:
3
3 4
.S..
###.
..E.
3 4
.S..
.E..
....
3 4
.S..
####
..E.
输出样例:
5
1
oop!
#include <iostream>
#include <algorithm>
#include <queue>
#include<cstring>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 210;
char a[N][N];
bool st[N][N];
int dis[N][N];//
int n, m, t;
void bfs(PII start, PII end) {
queue<PII> q;
q.push(start);
while (!q.empty()) {
PII u = q.front();
q.pop();
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int x1 = u.x + dx[i];
int y1 = u.y + dy[i];
if (a[x1][y1] == '#') continue;
if (a[x1][y1] == '.' && !st[x1][y1]) {
dis[x1][y1] = dis[u.x][u.y] + 1;
st[x1][y1] = true;
q.push({x1, y1});
}
if (a[x1][y1] == a[end.x][end.y]) {
cout << dis[u.x][u.y] + 1 << endl;
return;
}
}
}
cout << "oop!" << endl;
}
int main() {
cin >> t;
while (t--) {
PII start, end;
cin >> n >> m;
memset(a,'#',sizeof(a));//全部初始化为# 后面可以不做边界判断
memset(dis,0,sizeof(dis));
memset(st, 0, sizeof(st));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (a[i][j] == 'S') {
start.x = i;
start.y = j;
} else if (a[i][j] == 'E') {
end.x = i;
end.y = j;
}
}
bfs(start, end);
}
return 0;
}
地牢大师
你现在被困在一个三维地牢中,需要找到最快脱离的出路!
地牢由若干个单位立方体组成,其中部分不含岩石障碍可以直接通过,部分包含岩石障碍无法通过。
向北,向南,向东,向西,向上或向下移动一个单元距离均需要一分钟。
你不能沿对角线移动,迷宫边界都是坚硬的岩石,你不能走出边界范围。
请问,你有可能逃脱吗?
如果可以,需要多长时间?
输入格式
输入包含多组测试数据。
每组数据第一行包含三个整数 L,R,C 分别表示地牢层数,以及每一层地牢的行数和列数。
接下来是 L个 R行 C列的字符矩阵,用来表示每一层地牢的具体状况。
每个字符用来描述一个地牢单元的具体状况。
其中, 充满岩石障碍的单元格用”#”表示,不含障碍的空单元格用”.”表示,你的起始位置用”S”表示,终点用”E”表示。
每一个字符矩阵后面都会包含一个空行。
当输入一行为”0 0 0”时,表示输入终止。
输出格式
每组数据输出一个结果,每个结果占一行。
如果能够逃脱地牢,则输出”Escaped in x minute(s).”,其中X为逃脱所需最短时间。
如果不能逃脱地牢,则输出”Trapped!”。
数据范围
1≤L,R,C≤100
输入样例:
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
输出样例:
Escaped in 11 minute(s).
Trapped!
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N=110;
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};
char g[N][N][N];
bool vis[N][N][N];
int ans[N][N][N];
int n,r,c;
struct Point{
int x,y,z;
};
queue<Point> st;
int bfs(){
while(!st.empty()){
Point p = st.front();
st.pop();
for(int i=0;i<6;i++) {
int m_x=p.x+dx[i], m_y=p.y+dy[i], m_z=p.z+dz[i];
if(m_x<=n && m_y<=r && m_z<=c && m_x>=1 && m_y>=1 && m_z>=1 && !vis[m_x][m_y][m_z] && g[m_x][m_y][m_z]!='#') {
vis[m_x][m_y][m_z]=true;
ans[m_x][m_y][m_z]=ans[p.x][p.y][p.z]+1;
if(g[m_x][m_y][m_z]=='E') return ans[m_x][m_y][m_z];
st.push({m_x,m_y,m_z});
}
}
}
return -1;
}
int main() {
while(cin>>n>>r>>c && (n || r || c)) {
memset(vis, false, sizeof(vis));
memset(ans, 0, sizeof(ans));
while(!st.empty()) st.pop();
for(int i=1;i<=n;i++)
for(int j=1;j<=r;j++)
for(int k=1;k<=c;k++) {
cin>>g[i][j][k];
if(g[i][j][k]=='S') {
st.push({i,j,k});
vis[i][j][k]=true;
}
}
int cnt = bfs();
if(cnt != -1)
printf("Escaped in %d minute(s).\n",cnt);
else
cout<<"Trapped!"<<endl;
}
return 0;
}