题目集地址 Codeforces Round #766 (Div. 2)
A Not Shading 简单思维
题目地址:A Not Shading
题目大意:一个棋盘,n*m大小,有黑色方块和白色方块,对于每一个黑色方块可以执行如下操作,
将黑块所在行全部变为黑色
将黑块所在列全部变为黑色
给出当前棋盘黑白方块的状态和nmrc,问执行多少次操作可以将r c坐标的方块变为黑色,若不能变为黑色,输出-1。
思路:只要棋盘中有黑色块,就可以将任意方块变为黑色。
如果rc位置处的方块本身就是黑色就不需要执行操作了,输出0
AC代码:
#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
char str[60][60];
bool row[60];
bool colomn[60];
void solve()
{
int n,m,r,c;
scanf("%d%d%d%d",&n,&m,&r,&c);
char ch;
scanf("%c",&ch);
bool bf = false;
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
scanf("%c",&ch);
if(ch=='\n')
{
scanf("%c",&ch);
}
str[i][j]=ch;
if(ch=='B')
{
bf=true;
row[i]=true;
colomn[j]=true;
}
}
}
if(bf==false)
{
printf("-1\n");
}
else if(str[r][c]=='B')
{
printf("0\n");
}
else if(row[r]||colomn[c])
{
printf("1\n");
}
else
{
printf("2\n");
}
}
int main()
{
// freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
solve();
memset(str,0,sizeof(str));
memset(row,0,sizeof(row));
memset(colomn,0,sizeof(colomn));
}
return 0;
}
B Not Sitting 思维+搜索
题目地址:B Not Sitting
题目大意:有一个nm的板子,有两个人A和B,A可以禁止B坐其中k个位置,B想和A坐的最近,A想和B坐的最远,A先选择禁止的位置,然后B再选坐,A再选一个离B最远的位置,问当k分别为0,1,2,…,nm-1时他们的最远距离是多少
(舔狗不得house)
思路:已知B坐中间是最优的,因此A禁止也是先禁止中间,用bfs的思想从中间bfs到边缘,离中间越远,答案越大
因为n,m都是1e5大小的,所有不能直接开数组,这里我用了vector
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int nx[] = {1, 0, -1, 0};
int ny[] = {0, 1, 0, -1};
vector<int> M[N];
void solve()
{
int n, m;
cin >> n >> m;
queue<pair<int, int>> Q;
for (int i = 1; i <= n; i++)
M[i].resize(m + 1), M[i].clear();
map<int, int> ans;
for (int i = (n + 1) / 2; i <= (n + 2) / 2; i++)
for (int j = (m + 1) / 2; j <= (m + 2) / 2; j++)
{
Q.push({i, j});
M[i][j] = 1;
}
while (!Q.empty())
{
int nowX = Q.front().first;
int nowY = Q.front().second;
Q.pop();
ans[M[nowX][nowY]]++;
for (int i = 0; i < 4; i++)
{
int nextX = nowX + nx[i];
int nextY = nowY + ny[i];
int nextV = M[nowX][nowY] + 1;
if (nextX < 1 || nextY < 1 || nextX > n || nextY > m)
continue;
if (M[nextX][nextY] != 0)
continue;
M[nextX][nextY] = nextV;
Q.push({nextX, nextY});
}
}
int oriAns = n / 2 + m / 2;
for (auto it : ans)
{
if (it.second == 0)
continue;
int cnt = it.second;
for (int i = 1; i <= cnt; i++)
cout << oriAns << " ";
oriAns++;
}
cout << endl;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// int t = 1;
int t;
scanf("%d",&t);
while(t--)
{
solve();
}
return 0;
}
C Not Assigning
题目地址C Not Assigning