A - Grid Ice Floor
bfs,遍历四个方向,出发前检测一次,沿着一个方向走,每走一步又检测一次,若下一步会撞墙,则结束行走。
细节在于撞墙时点的入队和最后数量的统计。
#include<bits/stdc++.h>
#define int long long
int n, m, ans, vis[207][207];
char mp[207][207];
const int dx[5] = {0, 1, 0, -1, 0};
bool check(int x, int y) {
if (mp[x][y] != '#') return true;
else return false;
}
void bfs() {
queue <std::pair <int, int>> q;
q.push({2, 2});
vis[2][2] = 1; // 这个别漏了
while (!q.empty()) {
auto top = q.front(); q.pop();
int tx = top.first, ty = top.second;
for (int i = 0; i <= 3; ++ i) {
int x = tx, y = ty;
if (check(x + dx[i], y + dx[i + 1])) {
while (true) {
int nx = x + dx[i], ny = y + dx[i + 1];
if(check(nx, ny)) {
x = nx, y = ny;
vis[x][y] ++;
} else {
if (vis[x][y] <= 1) q.push({x, y});
break;
}
}
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
cin >> mp[i][j];
}
}
bfs();
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
if (vis[i][j] >= 1) ans ++;
}
}
cout << ans;
}
B - Strictly Superior
判断是否有商品满足下列条件
1.Pi等于Pj
2.j商品拥有i商品所有功能
3.Pi价格大于Pj,或者j商品拥有i商品所没有的功能
满足其一即可输出Yes
#include<bits/stdc++.h>
using namespace std;
bool b[105][105];
int a[105];
//void solve(int n,string s){
//}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,m,k,w=0;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>a[i]>>k;
for(int j=0;j<k;j++){
int sh;
cin>>sh;
b[i][sh]=1;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
w=0;
if(i==j){
continue;
}
if(a[i]>a[j]){
continue;
}
if(a[i]<a[j]){
for(int l=0;l<m;l++){
if(b[j][l]==1&&b[i][l]==0){
w=1;
}
}
if(!w){
cout<<"Yes";
return 0;
}
}
if(a[i]==a[j]){
for(int l=0;l<m;l++){
if(b[j][l]==1&&b[i][l]==0){
w=1;
break;
}
if(b[i][l]==1&&b[j][l]==0){
w=2;
}
}
if(w==2){
cout<<"Yes";
return 0;
}
}
}
}
cout<<"No";
}
C - Reversible
判断有多少个不同的字符串,反转的也算同一个
思路:用一个map存字符串是否出现过,若没出现过则sum++,同时存其及其反转后的字符串到map
#include<bits/stdc++.h>
using namespace std;
//void solve(int n,string s){
//}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,sum=0;
string s;
unordered_map<string,bool>v;
cin>>n;
while(n--){
cin>>s;
if(!v[s]){
v[s]=1;
reverse(s.begin(),s.end());
v[s]=1;
sum++;
}
}
cout<<sum;
}
E - Vacation Together
寻找最长的连续列的o
思路:sum表示当前最长连续列o,num表示上一个状态最长连续列o,每次连续停止时max两者最大即可,同时令sum=0
#include<bits/stdc++.h>
using namespace std;
bool solve(int j,int n,string a[]){
for(int i=0;i<n;i++){
if(a[i][j]=='x'){
return false;
}
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,m,sum=0,num=0;
cin>>n>>m;
//solve();
string a[105];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<m;i++){
if(solve(i,n,a)){
sum++;
}
else{
num=max(sum,num);
sum=0;
}
}
num=max(sum,num);
cout<<num;
}
H - When?
签到题,打表
#include<bits/stdc++.h>
using namespace std;
//void solve(){
//}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
if(n==0){
cout<<"21:00";
}
else if(n<10){
cout<<"21:0"<<n;
}
else if(n<60){
cout<<"21:"<<n;
}
else if(n>=60&&n<70){
cout<<"22:0"<<n-60;
}
else if(n>=70){
cout<<"22:"<<n-60;
}
}
I - Rotation
标记每次1操作之后的字符串的开头
#include<bits/stdc++.h>
using namespace std;
//void solve(int n,string s){
//}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,m,k=0,num;
string s;
cin>>n>>m;
cin>>s;
while(m--){
int q,w;
char ch;
cin>>q>>w;
if(q==2){
num=(k+w-1+n)%n;
cout<<s[num]<<endl;
}
else if(q==1&&w%n==0){
}
else {
k=k-w+n;
k%=n;
}
}
}
K - Many Oranges
可以看出,最大值由最小重量的橘子取得,因为余数总可以均摊到取的橘子上,向下取整。最小值由最大重量的橘子取得,因为余数总可以均摊到其他橘子上,向上取整
#include<bits/stdc++.h>
using namespace std;
//void solve(int n,string s){
//}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,m,max,min;
double k;
cin>>n>>m>>k;
k*=1000;
max=floor(k/n);
min=ceil(k/m);
if(min>max){
cout<<"UNSATISFIABLE"<<endl;
}
else{
cout<<min<<' '<<max<<endl;
}
}
L - Alcoholic
判断酒精含量之和是否大于X,是则输出第几次喝酒
思路:考虑到精度问题,让X*100即可
#include<bits/stdc++.h>
using namespace std;
long long n,m,q,sum,x=1,w;
int solve(){
cin>>q>>w;
sum+=q*w;
if(sum>m){
cout<<x<<endl;
return 1;
}
x++;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m;
m*=100;
while(n--){
if(solve()){
return 0;
}
}
cout<<"-1";
}