A.直接模拟即可,最后根据翻转次数奇偶判断。
B.类似连连看的判断,要求判断任意两点间是否能最多只转一次方向就相连。对每一点,先拓展出和它相同行,相同列能连到的点,再对那些点再做一次拓展,记录下总共能到达的点数目,判断是否为总点数即可。代码比较挫
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
const int maxn = 50 + 5;
const int INF = 1000000000;
typedef long long LL;
char M[maxn][maxn];
int vis[maxn][maxn];
struct Node{
int x,y;
};
int n,m;
queue<Node> Q;
int Bfs(int x,int y){
memset(vis,0,sizeof(vis));
while(!Q.empty()) Q.pop();
int ret = 1;
for(int i = y+1;i < m;i++){
if(M[x][i] == 'B'){
Q.push(Node{x,i});
if(vis[x][i] == 0){
ret++;
vis[x][i] = 1;
}
}
else break;
}
for(int i = y-1;i >= 0;i--){
if(M[x][i] == 'B'){
Q.push(Node{x,i});
if(vis[x][i] == 0){
vis[x][i] = 1;
ret++;
}
}
else break;
}
while(!Q.empty()){
int X = Q.front().x;
int Y = Q.front().y;
Q.pop();
for(int i = X-1;i >= 0;i--){
if(M[i][Y] == 'B'){
if(vis[i][Y] == 0){
ret++;
vis[i][Y] = 1;
}
}
else break;
}
for(int i = X+1;i < n;i++){
if(M[i][Y] == 'B'){
if(vis[i][Y] == 0){
vis[i][Y] = 1;
ret++;
}
}
else break;
}
}
while(!Q.empty()) Q.pop();
for(int i = x+1;i < n;i++){
if(M[i][y] == 'B'){
Q.push(Node{i,y});
if(vis[i][y] == 0){
vis[i][y] = 1;
ret++;
}
}
else break;
}
for(int i = x-1;i >= 0;i--){
if(M[i][y] == 'B'){
Q.push(Node{i,y});
if(vis[i][y] == 0){
vis[i][y] = 1;
ret++;
}
}
else break;
}
while(!Q.empty()){
int X = Q.front().x;
int Y = Q.front().y;
Q.pop();
for(int i = Y-1;i >= 0;i--){
if(M[X][i] == 'B'){
if(vis[X][i] == 0){
vis[X][i] = 1;
ret++;
}
}
else break;
}
for(int i = Y+1;i < m;i++){
if(M[X][i] == 'B'){
if(vis[X][i] == 0){
vis[X][i] = 1;
ret++;
}
}
else break;
}
}
return ret;
}
int main(){
while(cin >> n >> m){
int total = 0;
for(int i = 0;i < n;i++) scanf("%s",M[i]);
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++)
if(M[i][j] == 'B') total++;
}
int ans = 1;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(M[i][j] == 'B' && Bfs(i,j) != total){
//printf("%d %d %d\n",i,j,Bfs(i,j));
ans = 0;
break;
}
}
if(ans == 0) break;
}
if(ans == 0) cout << "NO\n";
else cout << "YES\n";
}
return 0;
}
C.贪心的做法,从小到大排序后,优先选小的,然后把这个数a*k的值标记为不能选的,因为数的范围很大,用个map即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
const int maxn = 100000 + 5;
const int INF = 1000000000;
typedef long long LL;
LL a[maxn];
map<LL,bool> M;
int main(){
int n;
LL k;
while(cin >> n >> k){
for(int i = 0;i < n;i++) cin >> a[i];
sort(a,a+n);
M.clear();
int ans = 0;
for(int i = 0;i < n;i++){
if(M.count(a[i]) == 0){
ans++;
M[a[i]*k] = true;
}
}
cout << ans << endl;
}
return 0;
}