袁小奔04-08文章
比赛传送门
| 题目 | 分数 |
|---|---|
总分:
全班第二,终于拿到一个理想的成绩了。
T4: P1141 01迷宫
源代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
struct P{
int x, y, step;
};
int n, m, Q, vis[N][N];
char a[N][N];
queue<P> q;
int bfs(int sx, int sy){
while(!q.empty()) q.pop();
memset(vis, 0, sizeof vis);
q.push({sx, sy, 1});
vis[sx][sy] = 1;
int s = 0;
while(!q.empty()){
int x = q.front().x, y = q.front().y;
s = max(s, q.front().step);
q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(nx < 1 || nx > n || ny < 1 || ny > m || vis[nx][ny] == 1 || a[x][y] == a[nx][ny]){
continue;
}
q.push({nx, ny, s + 1});
vis[nx][ny] = 1;
}
}
return s + 1;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> Q;
m = n;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; ++j){
cin >> a[i][j];
}
}
for(int i = 1, x, y; i <= Q; ++i){
cin >> x >> y;
cout << bfs(x, y) << '\n';
}
return 0;
}
看一眼数据:
- 对于
100\% 的数据,1\le n \le 1000 ,1\le m \le 100000 。
再算一下时间复杂度:
所以我们来一步步优化。
先把平方给优化掉。
也就是把那个memset给他去掉。
但是把
memset给他去掉之后,那后面如果再进行下一个搜索的时候,发现这个地方被走过了,可是正确答案又却又需要走这个地方,那该怎么办?
好问题
所以我们就需要把他走过的点给他记录下来,免得以后发生这种情况,如果真的遇见这种情况就直接输出步数,就不用搜索。
怎么记?
当然是新开一个数组记啦。
俺当然知道是用数组记,俺问的是他在搜索的过程中,他走过的点怎么记。
这个时候我们需要开一个队列,在搜索的过程中把他走过的点全部都给弹进去,最后在循环结束的时候,出来把里面所有的点都给他打上标记,然而对应的步数就是这个队列最开始循环完之后的长度。
重点过完了,接下来就是基操了。
在
so......
AC Code:
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define P pair<int, int>
using namespace std;
const int N = 1e3 + 5;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int n, m, Q, dis[N][N];
char a[N][N];
queue<P> q, v;
void bfs(int sx, int sy){
q.emplace(sx, sy);
//v.emplace(sx, sy);
//这里千万不能多加一步,不然的话输出的步数就会多一步哦。
dis[sx][sy] = 1;
while(!q.empty()){
int x = q.front().x, y = q.front().y;
v.emplace(x, y);
q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(nx < 1 || nx > n || ny < 1 || ny > n || dis[nx][ny] != -1 || a[x][y] == a[nx][ny]){
continue;
}
q.emplace(nx, ny);
dis[nx][ny] = 1;
}
}
int cnt = v.size();
while(!v.empty()){
dis[v.front().x][v.front().y] = cnt;
//cout << v.front().x << ' ' << v.front().y << '\n';
v.pop();
}
return;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> Q;
m = n;
memset(dis, -1, sizeof dis);
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> a[i][j];
}
}
for(int i = 1, x, y; i <= Q; ++i){
cin >> x >> y;
if(dis[x][y] == -1) bfs(x, y);
cout << dis[x][y] << '\n';
}
return 0;
}
T5: P11380 [GESP202412 八级] 排队
源代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
int n, m, fa[N];
int find(int x){
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int fact(int x){
if(x == 1) return 1;
return x * fact(x - 1) % mod;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; ++i) fa[i] = i;
for(int i = 1, x, y; i <= m; ++i){
cin >> x >> y;
int t1 = find(x), t2 = find(y);
if(t1 == t2){
cout << 0;
return 0;
}
else{
fa[t1] = t2;
}
}
cout << fact(n - m);
return 0;
}
错误原因:举个栗子
4 2
1 2
1 3
虽然没有成环,但是也矛盾了。(完美打击我的代码[吐血])
所以我们需要增加两个数组,分别来存每一个变量的左边是谁?右边是谁?如果左边或者右边没有的话则数组该位置为0。
如果出题者丧心病狂,故意给你整输入重复的呢。照道理说,是不会产生矛盾的,但代码判断的话却会出现矛盾。
好问题
不过处理方式也挺简单的,只要加入特判或set即可。
AC Code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
int n, m, fa[N], l[N], r[N];
int find(int x){
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int fact(int x){
if(x == 1) return 1;
return x * fact(x - 1) % mod;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; ++i) fa[i] = i;
for(int i = 1, x, y; i <= m; ++i){
cin >> x >> y;
int t1 = find(x), t2 = find(y);
if((l[t2] != t1 && l[t2] != 0) || (r[t1] != t2 && r[t1] != 0) || t1 == t2){
cout << 0;
return 0;
}
else{
fa[t1] = t2;
l[t2] = t1, r[t1] = t2;
}
}
int cnt = 0;
for(int i = 1; i <= n; ++i){
if(fa[i] == i) ++cnt;
}
cout << fact(cnt);
return 0;
}
T6: P1205 [USACO1.2] 方块转换 Transformations
源代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
int n;
char a[N][N], b[N][N], A[N][N];
bool work1(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[n - j + 1][i];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(A[i][j] != b[i][j]) return false;
}
}
return true;
}
bool work2(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[n - i + 1][n - j + 1];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(A[i][j] != b[i][j]) return false;
}
}
return true;
}
bool work3(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[j][n - i + 1];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(A[i][j] != b[i][j]) return false;
}
}
return true;
}
bool work4(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[n - i + 1][j];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(A[i][j] != b[i][j]) return false;
}
}
return true;
}
bool work5(){
int B[N][N];
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[n - i + 1][j];
B[i][j] = a[i][j];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
a[i][j] = A[i][j];
}
}
if(work1()) return true;
else if(work2()) return true;
else if(work3()) return true;
return false;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
a[i][j] = B[i][j];
}
}
}
bool work6(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(a[i][j] != b[i][j]) return false;
}
}
return true;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> a[i][j];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> b[i][j];
}
}
if(work1()) cout << 1;
else if(work2()) cout << 2;
else if(work3()) cout << 3;
else if(work4()) cout << 4;
else if(work5()) cout << 5;
else if(work6()) cout << 6;
else cout << 7;
return 0;
}
其实挺简单的,就一个大模拟。
是因为第4种方式的理解问题,导致我痛失30分(在此也警戒各位宝子们,主科一定要学好啊)。
AC Code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
int n;
char a[N][N], b[N][N], A[N][N];
bool work1(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[n - j + 1][i];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(A[i][j] != b[i][j]) return false;
}
}
return true;
}
bool work2(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[n - i + 1][n - j + 1];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(A[i][j] != b[i][j]) return false;
}
}
return true;
}
bool work3(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[j][n - i + 1];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(A[i][j] != b[i][j]) return false;
}
}
return true;
}
bool work4(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[i][n - j + 1];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(A[i][j] != b[i][j]) return false;
}
}
return true;
}
bool work5(){
int B[N][N];
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
A[i][j] = a[i][n - j + 1];
B[i][j] = a[i][j];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
a[i][j] = A[i][j];
}
}
if(work1()) return true;
else if(work2()) return true;
else if(work3()) return true;
return false;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
a[i][j] = B[i][j];
}
}
}
bool work6(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(a[i][j] != b[i][j]) return false;
}
}
return true;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> a[i][j];
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> b[i][j];
}
}
if(work1()) cout << 1;
else if(work2()) cout << 2;
else if(work3()) cout << 3;
else if(work4()) cout << 4;
else if(work5()) cout << 5;
else if(work6()) cout << 6;
else cout << 7;
return 0;
}
乾坤未定,你我皆是黑马······