牛客周赛 Round 36
A.小红的数位删除 https://ac.nowcoder.com/acm/contest/76609/A
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 1; i <= 3; i++) n /= 10;
cout << n;
return 0;
}
B.小红的小红矩阵构造https://ac.nowcoder.com/acm/contest/76609/B
异或,两数的二进制相同输出0,不同输出1
两次循环分别固定行和列
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
set<int> q;
int n, m, x, sum = 0;
int g[N][N];
bool flag = 1;
int main(){
cin >> n >> m >> x;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> g[i][j];
sum += g[i][j];
}
}
if(sum != x) flag = 0;
for(int i = 0; i < n; i++){
int temp = 0;
for(int j = 0; j < m; j++){
temp ^= g[i][j];
}
q.insert(temp);
}
for(int i = 0; i < m; i++){
int temp = 0;
for(int j = 0; j < n; j++){
temp ^= g[j][i];
}
q.insert(temp);
}
if(q.size() != 1) flag = 0;
if(flag) puts("accepted");
else puts("wrong answer");
return 0;
}
C.小红的白色字符串 https://ac.nowcoder.com/acm/contest/76609/C
规律是大写字母的前一个格为空白
#include <bits/stdc++.h>
using namespace std;
string s;
int len, ans = 0;
bool space[200005];
int main(){
cin >> s;
len = s.size();
for(int i = 1; i < len; i++){
if(s[i] < 'a' && !space[i - 1]){
ans ++;
space[i] = 1;
}
}
cout << ans;
return 0;
}
D.小红走矩阵 https://ac.nowcoder.com/acm/contest/76609/D
bfs,记录上一格的字符要用t.first & t.second
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n, m, ans = 0;
char g[N][N];
bool st[N][N];
int f[N][N];
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
void bfs(int a, int b){
st[a][b] = true;
queue<PII> q;
q.push({a, b});
while(q.size()){
PII t = q.front();
q.pop();
for(int i = 0; i < 4; i++){
int x = dx[i] + t.first, y = dy[i] + t.second;
if(x >= 0 && x < n && y >= 0 && y < m && !st[x][y] && g[x][y] != g[t.first][t.second]){
f[x][y] = f[t.first][t.second] + 1;
st[x][y] = true;
q.push({x, y});
}
}
}
}
int main(){
memset(f, 0, sizeof f);
memset(st, 0, sizeof st);
cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> g[i][j];
bfs(0, 0);
if(st[n - 1][m - 1]) cout << f[n - 1][m - 1];
else cout << "-1";
return 0;
}