题解 | #H 本题主要考察了DFS#2023牛客寒假算法基础集训营1

题目网址

https://ac.nowcoder.com/acm/contest/46800/H

附图(点击查看大图)

这题也没啥花里胡哨的

不过本人没用DFS

观察题目可以发现所有拼图的1和2缺口的总和数量是一样

思路是记录1和2缺口的数量,然后计算就好

#include <iostream>
using namespace std;

int main() {
    int t, n, a, b, x, y;
    string m;
    cin>>t;
    while(t--){
        cin>>n;
        x=0;y=0;
        n=n*n-1;
        while(n--){
            cin>>m;
            for(int i=0;i<4;i++){
                if(m[i]=='1'){
                    x++;
                }else if(m[i]=='2'){
                    y++;
                }
            }
        }
        cout<<10+x-y<<endl; 
    } 
}

猜你喜欢

转载自blog.csdn.net/weixin_45940369/article/details/128709831