CF1873C Target Practice 题解

· · 题解

Description

有一个 10\times10 的标靶,最外环为 1 分,其次是 2 分,依此类推,最内环是 5 分。

给定一个 10\times 10 的矩阵 s,如果 s_{i,j}X,说明射中了靶标上的 (i,j) ,求总得分。

T 组数据。

Solution

我们模拟得分的过程即可。

设射中 (x,y)

如果 x=1\space or\space y=1\space or\space x=10\space or\space y=10,那它一定在一分区。

如果 x=2\space or\space y=2\space or\space x=9\space or\space y=9,那它一定在两分区。可能会问 (2,10) 不在两分区啊,但 y=10 的情况已经被判掉了,在 8\times8 的范围内一定在最外环。

这样重复到最内环。

实际上这个过程就是不断将范围缩小,一个递归的过程,外环一定比内环条件更少,更容易满足,适合锻炼分支结构和递归思想。

Code

#include<bits/stdc++.h>
using namespace std;
int t;
int check(int x,int y){
    if(x==1||y==1||x==10||y==10) return 1;
    if(x==2||y==2||x==9||y==9) return 2;
    if(x==3||y==3||x==8||y==8) return 3;
    if(x==4||y==4||x==7||y==7) return 4;
    if(x==5||y==5||x==6||y==6) return 5;
}
void solve(){
    int ans=0;
    for(int i=1;i<=10;i++){
        string s;
        cin>>s;
        for(int j=0;j<10;j++){
            if(s[j]=='X') ans+=check(i,j+1);
        }
    }
    cout<<ans<<endl;
}
int main(){
    cin>>t;
    while(t--){
        solve();
    }   
    return 0;
}