2022/1/11总结

今天上午有点自闭,链表最后一题硬是做不出来,疑似出现了死循环但是调试了1个多小时硬是没有找到。于是决定下午去做搜索。

也不知道今天是不是状态不太好,搜索也没做几道出来。

今天总学习时长10h:

早点休息吧,希望明天状态好点能把题目做出来。


题目描述

由数字00组成的方阵中,有一任意形状闭合圈,闭合圈由数字11构成,围圈时只走上下左右44个方向。现要求把闭合圈内的所有空间都填写成22.例如:6 \times 66×6的方阵(n=6n=6),涂色前和涂色后的方阵如下:

0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1

输入格式

每组测试数据第一行一个整数n(1≤n≤30)

接下来n行,由0和1组成的n×n的方阵。

方阵内只有一个闭合圈,圈内至少有一个00。

输出格式

已经填好数字22的完整方阵。

输入输出样例

输入 #1复制

6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1

输出 #1复制

0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1

说明/提示

1≤n≤30

这道题由学长提示。可以由外向内思考,从边境搜索到中间,碰到墙返回。这样遍历一遍后,就剩下中间被1包围的部分没有染色,最后反向处理一下即可。

#include <bits/stdc++.h>

using namespace std;

int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};

int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	int vis[n + 5][n + 5] = {0};
	int mp[n + 5][n + 5];
	for(int i = 1;i <= n;i ++)
	{
		for(int j = 1;j <= n;j ++)
		{
			cin >> mp[i][j];
		}
	}
	queue<int> qx, qy;
	for(int i = 1;i <= n;i ++)
	{
		qx.push(1);
		qy.push(i);
	}
	for(int i = 1;i <= n;i ++)
	{
		qx.push(i);
		qy.push(1);
	}
	for(int i = 1;i <= n;i ++)
	{
		qx.push(n);
		qy.push(i);
	}
	for(int i = 1;i <= n;i ++)
	{
		qx.push(i);
		qy.push(n);
	}
	while(!qx.empty())
	{
		int nx, ny;
		for(int i = 0;i < 4;i ++)
		{
			if(mp[qx.front()][qy.front()] == 1) continue;
			nx = qx.front() + dx[i];
			ny = qy.front() + dy[i];
			if(nx >= 1 && nx <= n && ny >= 1 && ny <= n && !vis[nx][ny] && mp[nx][ny] == 0)
			{
				vis[nx][ny] = 1;
				mp[nx][ny] = 2;
				qx.push(nx);
				qy.push(ny);
			}
		}
		qx.pop();
		qy.pop();
	}
	for(int i = 1;i <= n;i ++)
	{
		for(int j = 1;j <= n;j ++)
		{
//			cout << mp[i][j] << " ";
			if(mp[i][j] == 2) cout << "0 ";
			else if(mp[i][j] == 0) cout << "2 ";
			else cout << mp[i][j] << " "; 
		}
		cout << endl;
	}
	return 0;
}

题目描述

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. Given a diagram of Farmer John's field, determine how many ponds he has.

由于近期的降雨,雨水汇集在农民约翰的田地不同的地方。我们用一个NxM(1<=N<=100;1<=M<=100)网格图表示。每个网格中有水('W') 或是旱地('.')。一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑。约翰想弄清楚他的田地已经形成了多少水坑。给出约翰田地的示意图,确定当中有多少水坑。

输入格式

Line 1: Two space-separated integers: N and M * Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

第1行:两个空格隔开的整数:N 和 M 第2行到第N+1行:每行M个字符,每个字符是'W'或'.',它们表示网格图中的一排。字符之间没有空格。

输出格式

Line 1: The number of ponds in Farmer John's field.

一行:水坑的数量

输入输出样例

输入 #1复制

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

输出 #1复制

3

说明/提示

OUTPUT DETAILS: There are three ponds: one in the upper left, one in the lower left, and one along the right side.

这道题,就是八方向访问+标记,遍历整个地图得到“整块”的数量,得到最终答案。这里使用bfs:

#include <bits/stdc++.h>

using namespace std;

int vis[200][200] = {0};
int mp[200][200];

int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {1, -1, 0, 0, 1, -1, -1, 1};

int main()
{
	int N, M;
	cin >> N >> M;
	getchar();
	for(int i = 1;i <= N;i ++)
	{
		string tmp;
		getline(cin, tmp, '\n');
		for(int k = 0;k < tmp.size();k ++)
		{
			char c = tmp[k];
			if(c == 'W') mp[i][k + 1] = 1;
			else mp[i][k + 1] = 0;	
		}
	}
//	for(int i = 1;i <= N;i ++)
//	{
//		for(int j = 1;j <= M;j ++)
//		{
//			cout << mp[i][j] << " ";
//		}
//		cout << endl;
//	}
	int sum = 0;
	for(int i = 1;i <= N;i ++)
	{
		for(int j = 1;j <= M;j ++)
		{
			if(vis[i][j] != 1 && mp[i][j] == 1)
			{
				queue<int> qx, qy;
				qx.push(i);
				qy.push(j);
				vis[i][j] = 1;
				while(!qx.empty())
				{
					int nx, ny;
					for(int k = 0;k < 8;k ++)
					{
						nx = qx.front() + dx[k];
						ny = qy.front() + dy[k];
						if(nx >= 1 && nx <= N && ny >= 1 && ny <= M && vis[nx][ny] != 1 && mp[nx][ny] == 1)
						{
							qx.push(nx);
							qy.push(ny);
							vis[nx][ny] = 1;
						}
					}	
					qx.pop();
					qy.pop();				
				}
				sum ++;
			}
		}
	}
//	cout << endl;
//	for(int i = 1;i <= N;i ++)
//	{
//		for(int j = 1;j <= M;j ++)
//		{
//			cout << vis[i][j] << " ";
//		}
//		cout << endl;
//	}
	cout << sum;
	return 0;
}

链表我真的不太擅长(脑袋都要指傻了)。明天再试试链表吧,然后把搜索任务尽量完成掉。今天的状态真的不太好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ISansXI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值