目录
前言:
题目描述:
有一个仅由数字0与1组成的n ×n格迷宫。若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上。
你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身)。
范围:
n≤1000,m≤100000
1.普通人思维
直接dfs,上代码:
#include<iostream>
#include<cstring>
using namespace std;
int n, m;
bool z[1000][1000];
bool own[1000][1000];
int func(int x, int y, bool last, int sum = 0) {
if (last == z[x][y] && !own[x][y]) {
own[x][y] = 1;
sum++;
}
else return sum;
if (last == 1)last = 0;
else last = 1;//模拟1转0,0转1的过程
if (x + 1 < n) {
if (own[x + 1][y] == 0&&z[x + 1][y ] == last) {
sum += func(x + 1, y, last);
}
}
if (y + 1 < n) {
if (own[x][y + 1] == 0 && z[x ][y+1] == last) sum += func(x, y + 1, last);
}
if (x - 1 >= 0) {
if (own[x - 1][y] == 0 && z[x - 1][y] == last) {
sum += func(x - 1, y, last);
}
}
if (y - 1 >= 0 && own[x][y - 1] == 0 && z[x ][y - 1] == last) {
sum += func(x , y-1, last);
}
//分别模拟上下左右的联通路径
return sum;//返回路径数量
}
int main() {
cin >> n >> m;
char ch;
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
cin >> ch;
z[x][y] = ch - '0';//读入二维数组
}
}
for (int i = 0; i < m; i++) {
int x, y;
memset(own, 0, sizeof(own));
cin >> x >> y;
cout << func(x - 1, y - 1, z[x-1][y-1]) << endl;
}
return 0;
}
提交上去,70分(血的教训)
2.第一次优化
优化方案:
1.定义了一个ans数组,递归找到答案后将答案存入answer,再将输入数据所在区域的所有数字赋值成answer
2.使用printf来提升输出速度
?
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n, m;
bool z[1000][1000];
bool own[1000][1000];
int ans[1000][1000]; int answer;
int func2(int x, int y, bool last) {
if (last == z[x][y]) {
own[x][y] = 1;
ans[x][y] = answer;
}
else {
return 0;
}if (last == 1)last = 0;
else last = 1;
if (x + 1 < n) {
if (!own[x + 1][y]) {
func2(x + 1, y, last);
}
}
if (y + 1 < n) {
if (!own[x][y + 1]) func2(x, y + 1, last);
}
if (x - 1 >= 0) {
if (!own[x - 1][y]) {
func2(x - 1, y, last);
}
}
if (y - 1 >= 0 && own[x][y - 1] == 0) {
func2(x, y - 1, last);
}
return 0;
}
int func(int x, int y, bool last, bool cishu = 0) {
int sum = 0;
if (!last-z[x][y]) {
own[x][y] = 1;
sum++;
}
else return sum;
if (last)last = 0;
else last = 1;
if (x + 1 < n) {
if (!own[x + 1][y] ) {
sum += func(x + 1, y, last, 1);
}
}
if (y + 1 < n) {
if (!own[x][y + 1]) sum += func(x, y + 1, last, 1);
}
if (x - 1 >= 0) {
if (!own[x - 1][y] ) {
sum += func(x - 1, y, last, 1);
}
}
if (y - 1 >= 0 && !own[x][y - 1] ) {
sum += func(x, y - 1, last, 1);
}
answer = sum;
return answer;
}
int main() {
cin >> n >> m;
char ch;
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
cin >> ch;
z[x][y] = ch - '0';
}
}int sum;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!ans[i][j]) {
answer = func(i, j, z[i][j]);
memset(own, 0, sizeof(own));
func2(i, j, z[i][j]);
memset(own, 0, sizeof(own));
}
}
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
if (ans[x - 1][y - 1])printf("%d\n", ans[x - 1][y - 1]);
else {
printf("%d\n", func(x - 1, y - 1, z[x - 1][y - 1]));
memset(own, 0, sizeof(own));
func2(x - 1, y - 1, z[x - 1][y - 1]);
}
}
return 0;
}
?
得分:80
3.第二次优化
优化方案:
1.将cishu参数省去了
2.将x,y的类型从int改为short,同时简化了很多操作来提升效率
3.将部分输入从cin改为scanf
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n, m;
bool z[1000][1000];
bool own[1000][1000];
int ans[1000][1000]; int answer;
int func2(int x, int y, bool last) {
if (!last - z[x][y]) {
own[x][y] = 1;
ans[x][y] = answer;
}
else {
return 0;
}if (last)last = 0;
else last = 1;
if (x + 1 < n) {
if (!own[x + 1][y]) {
func2(x + 1, y, last);
}
}
if (y + 1 < n) {
if (!own[x][y + 1]) func2(x, y + 1, last);
}
if (x - 1 >= 0) {
if (!own[x - 1][y]) {
func2(x - 1, y, last);
}
}
if (y - 1 >= 0 && own[x][y - 1] == 0) {
func2(x, y - 1, last);
}
return 0;
}
int func(short x, short y, bool last) {
int sum = 0;
if (!last - z[x][y]) {
own[x][y] = 1;
sum++;
}
else return sum;
if (last)last = 0;
else last = 1;
if (x + 1 < n) {
if (!own[x + 1][y]) {
sum += func(x + 1, y, last);
}
}
if (y + 1 < n) {
if (!own[x][y + 1]) sum += func(x, y + 1, last);
}
if (x - 1 >= 0) {
if (!own[x - 1][y]) {
sum += func(x - 1, y, last);
}
}
if (y - 1 >= 0 && !own[x][y - 1]) {
sum += func(x, y - 1, last);
}
answer = sum;
return answer;
}
int main() {
cin >> n >> m;
char ch;
for (int x = 0; x < n; ++x) {
for (int y = 0; y < n; ++y) {
cin >> ch;
z[x][y] = ch - '0';
}
}int sum;
for (int i = 0; i < m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
if (ans[x - 1][y - 1])printf("%d\n", ans[x - 1][y - 1]);
else {
printf("%d\n", func(x - 1, y - 1, z[x - 1][y - 1]));
memset(own, 0, sizeof(own));
func2(x - 1, y - 1, z[x - 1][y - 1]);
}
}
return 0;
}
得分:90
4.一种自创玄学做法
优化方案:
1.将x,y的类型从int改成short
2.同时减去了dfs2的last参数
3.将func2的类型从int变成void
4.玄学,将sum++等改为了++sum
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n, m;
bool z[1000][1000];
bool own[1000][1000];
int ans[1000][1000]; int answer;
int func2(int x, int y, bool last) {
if (!last - z[x][y]) {
own[x][y] = 1;
ans[x][y] = answer;
}
else {
return 0;
}if (last)last = 0;
else last = 1;
if (x + 1 < n) {
if (!own[x + 1][y]) {
func2(x + 1, y, last);
}
}
if (y + 1 < n) {
if (!own[x][y + 1]) func2(x, y + 1, last);
}
if (x - 1 >= 0) {
if (!own[x - 1][y]) {
func2(x - 1, y, last);
}
}
if (y - 1 >= 0 && own[x][y - 1] == 0) {
func2(x, y - 1, last);
}
return 0;
}
int func(short x, short y, bool last) {
int sum = 0;
if (!last - z[x][y]) {
own[x][y] = 1;
sum++;
}
else return sum;
if (last)last = 0;
else last = 1;
if (x + 1 < n) {
if (!own[x + 1][y]) {
sum += func(x + 1, y, last);
}
}
if (y + 1 < n) {
if (!own[x][y + 1]) sum += func(x, y + 1, last);
}
if (x - 1 >= 0) {
if (!own[x - 1][y]) {
sum += func(x - 1, y, last);
}
}
if (y - 1 >= 0 && !own[x][y - 1]) {
sum += func(x, y - 1, last);
}
answer = sum;
return answer;
}
int main() {
cin >> n >> m;
char ch;
for (int x = 0; x < n; ++x) {
for (int y = 0; y < n; ++y) {
cin >> ch;
z[x][y] = ch - '0';
}
}int sum;
for (int i = 0; i < m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
if (ans[x - 1][y - 1])printf("%d\n", ans[x - 1][y - 1]);
else {
printf("%d\n", func(x - 1, y - 1, z[x - 1][y - 1]));
memset(own, 0, sizeof(own));
func2(x - 1, y - 1, z[x - 1][y - 1]);
}
}
return 0;
}
得分:90~100(100比较少见)
5.满分代码
优化方案:
1.不使用memset
通过分析时间,我们可以得出memset所需时间相当长
因此不再使用memset
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n, m;
bool z[1000][1000];
bool own[1000][1000];
int ans[1000][1000]; int answer;
void func2(short x, short y) {//如果是这一块则记录答案
ans[x][y] = answer;
if (x + 1 < n&&own[x+1][y]&&!ans[x+1][y]) {
func2(x + 1, y);
}
if (y + 1 < n&&own[x][y+1]&&!ans[x][y+1]) {
func2(x, y + 1);
}
if (x - 1 >= 0&&own[x-1][y]&&!ans[x-1][y]) {
func2(x - 1,y);
}
if (y - 1 >= 0 &&own[x][y-1]&&!ans[x][y-1]) {
func2(x, y - 1);
}
return;
}
int func(short x, short y, bool last) {
int sum = 0;
if (!last - z[x][y]) {
own[x][y] = 1;
++sum;
}
else {
own[x][y]=0;//新增代码,防止溢出
return sum;
}
if (last)last = 0;
else last = 1;
if (x + 1 < n && !own[x + 1][y]) {
sum += func(x + 1, y, last);
}
if (y + 1 < n && !own[x][y + 1]) {
sum += func(x, y + 1, last);
}
if (x - 1 >= 0 && !own[x - 1][y]) {
sum += func(x - 1, y, last);
}
if (y - 1 >= 0 && !own[x][y - 1]) {
sum += func(x, y - 1, last);
}
return sum;
}
int main() {
cin >> n >> m;
char ch;
for (int x = 0; x < n; ++x) {
for (int y = 0; y < n; ++y) {
cin >> ch;
z[x][y] = ch - '0';
}
}
for (int i = 0; i < m; ++i) {
short x, y;
scanf("%hd%hd", &x, &y);
if (ans[x - 1][y - 1])printf("%d\n", ans[x - 1][y - 1]);
else {
answer=func(x - 1, y - 1, z[x - 1][y - 1]);
printf("%d\n",answer );
func2(x - 1, y - 1);
}
}
return 0;
}
一道普及-的题,耗了半天,只能说我太蒻了