题意很好懂,大致就是三种颜色,红和蓝一起会变绿,给个终态矩阵,问从原始状态到终态最少画几笔? 按一定规则画
思路就是记红为1,蓝为2,绿为3,先遍历绿色,针对每一块绿色进行删除,每找到一块绿色,首先将它置零,然后主对角线上和它挨着的红色或绿色-1,副对角线上和它挨着的蓝色或绿色-2,ans+2; 知道所有绿色遍历完为止。然后在遍历剩余的红色和蓝色,红色块主对角线上和它挨着的红色-1(包括它)ans+1,绿色副对角线上和它挨着的蓝色-2(包括它)ans+1。 最后输出ans即可。
#include<cstdio> #include<cstring> #include<iostream> using namespace std; int n,m; int g[51][51]; int ans=0; void dfs_1(int x,int y) //左上 { while(g[x][y]==1||g[x][y]==3) { // puts("1"); g[x][y]--; x--; y--; } } void dfs_2(int x,int y) // 右下 { while(g[x][y]==1||g[x][y]==3) { // puts("1"); g[x][y]--; x++; y++; } } void dfs_3(int x,int y) // 左下 { while(g[x][y]==2||g[x][y]==3) { // puts("1"); g[x][y]-=2; x++; y--; } } void dfs_4(int x,int y) // 右上 { while(g[x][y]==2||g[x][y]==3) { // puts("1"); g[x][y]-=2; x--; y++; } } int main() { int t; scanf("%d",&t); while(t--) { ans=0; memset(g,0,sizeof(g)); scanf("%d",&n); for(int i=1;i<=n;i++) { char cc[51]; memset(cc,'