BZOJ1102(搜索)
2024-09-08 05:37:06
随便写一下的搜索,别的OJ深搜就过了,强大的BZOJ成功栈溢出RE了我并使我屈服地用广搜过掉,第一行手动开栈惨遭无视。
广搜:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <fstream>
#include <bitset>
#define init(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define irep(i, a, b) for (int i = a; i >= b; i--)
using namespace std; typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18; template <typename T> void read(T &x) {
x = ;
int s = , c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') s = -;
for (; isdigit(c); c = getchar())
x = x * + c - ;
x *= s;
} template <typename T> void write(T x) {
if (x < ) x = -x, putchar('-');
if (x > ) write(x / );
putchar(x % + '');
} template <typename T> void writeln(T x) {
write(x);
puts("");
} const int maxn = 1e3 + ;
int n, A[maxn][maxn], up, down;
bool vis[maxn][maxn]; int bfs(int x, int y) {
queue<P> Q;
Q.push(P(x, y));
vis[x][y] = true;
bool small = true, equal = true, large = true;
while (!Q.empty()) {
int i = Q.front().first, j = Q.front().second;
Q.pop();
rep(a, -, ) rep(b, -, ) {
int nx = i + a, ny = j + b;
if (nx < || nx > n || ny < || ny > n) continue; int val = A[nx][ny], k = -;
if (val == A[i][j]) {
if (vis[nx][ny]) continue;
Q.push(P(nx, ny));
vis[nx][ny] = true;
} else if (val > A[i][j]) {
k = ;
} else k = ; if (k == ) small = false, equal = false;
else if (k == ) large = false, equal = false;
}
}
if (equal) return ;
if (small) return ;
if (large) return ;
return -;
} int main() {
read(n);
rep(i, , n) rep(j, , n) read(A[i][j]);
rep(i, , n) rep(j, , n) {
if (!vis[i][j]) {
int k = bfs(i, j);
if (k == ) {
up++, down++;
} else if (k == ) {
up++;
} else if (k == ) {
down++;
}
}
}
printf("%d %d\n", up, down);
return ;
}
深搜:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <fstream>
#include <bitset>
#define init(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define irep(i, a, b) for (int i = a; i >= b; i--)
using namespace std; typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18; template <typename T> void read(T &x) {
x = ;
int s = , c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') s = -;
for (; isdigit(c); c = getchar())
x = x * + c - ;
x *= s;
} template <typename T> void write(T x) {
if (x < ) x = -x, putchar('-');
if (x > ) write(x / );
putchar(x % + '');
} template <typename T> void writeln(T x) {
write(x);
puts("");
} const int maxn = 1e3 + ;
int n, A[maxn][maxn], up, down;
bool vis[maxn][maxn]; int dfs(int i, int j) {
vis[i][j] = true;
bool small = true, equal = true, large = true;
rep(a, -, ) rep(b, -, ) {
int nx = i + a, ny = j + b;
if (nx < || nx > n || ny < || ny > n) continue; int val = A[nx][ny], k = -;
if (val == A[i][j]) {
if (vis[nx][ny]) continue;
k = dfs(nx, ny);
} else if (val > A[i][j]) {
k = ;
} else k = ; if (k == ) small = false, equal = false;
else if (k == ) large = false, equal = false;
else if (k == ) small = false, large = false;
else small = false, equal = false, large = false;
}
if (equal) return ;
if (small) return ;
if (large) return ;
return -;
} int main() {
read(n);
rep(i, , n) rep(j, , n) read(A[i][j]);
rep(i, , n) rep(j, , n) {
if (!vis[i][j]) {
int k = dfs(i, j);
if (k == ) {
up++, down++;
} else if (k == ) {
up++;
} else if (k == ) {
down++;
}
}
}
printf("%d %d\n", up, down);
return ;
}
最新文章
- Intellij Idea中定制getter setter的模板
- Y+的一些讨论
- EditorGUILayout.EnumPopup 枚举弹出选择菜单
- Unique Paths | &; ||
- UltraEdit 列模式
- 关于egit的日常操作总结
- 万网免费主机wordpress快速建站教程-wordpress下载及安装
- VB 增强的部件与引用
- Python快速入门(5)
- AndroidTv开发中常用的adb命令
- @Scheduled不执行的原因
- HashTable和HashMap的区别详解(转)
- 插件 DataTable 创建列表 render参数的详解与如何传递本行数据id
- 【linux】ubuntu下crontab无效解决方法
- 如何在Angular优雅编写HTTP请求
- ODAC(V9.5.15) 学习笔记(九)TOraSQLMonitor
- Java自动装箱中的缓存原理
- 安装postgresql之后为什么找不到postgresql service
- PHP扩展--XHProf优化PHP程序
- iOS开发API常用英语名词
热门文章
- 激活win10企业长期服务版
- 卡特兰数 HDU2067 &; HDU4165 &; HDU1134
- Gym - 100187A A - Potion of Immortality —— 贪心
- 步入C编程的第一天
- PL/SQL DEVELOPER执行计划的查看
- jQuery ajax序列化函数
- 并不对劲的CTS2019
- vue中的 v-if VS v-show
- bzoj 3992 [SDOI2015] 序列统计 —— NTT (循环卷积+快速幂)
- 微信小程序的ajax数据请求wx.request