zoukankan      html  css  js  c++  java
  • TZOJ 4267 An Easy Puz(深搜)

    描述

    Wddpdh find an interesting mini-game in the BBS of WHU, called “An easy PUZ”. It’s a 6 * 6 chess board and each cell has a number in the range of 0 and 3(it can be 0, 1, 2 or 3). Each time you can choose a number A(i, j) in i-th row and j-th column, then the number A(i, j) and the numbers around it (A(i-1, j), A(i+1, j),A(i, j-1),A(i, j+1), sometimes there may just be 2 or 3 numbers.) will minus 1 (3 to 2, 2 to 1, 1 to 0, 0 to 3). You can do it finite times. The goal is to make all numbers become 0. Wddpdh now come up with an extended problem about it. He will give you a number N (3 <= N <= 6) indicate the size of the board. You should tell him the minimum steps to reach the goal.

    输入

    The input consists of multiple test cases. For each test case, it contains a positive integer N(3 <= n <= 6). N lines follow, each line contains N columns indicating the each number in the chess board.

    输出

    For each test case, output minimum steps to reach the goal. If you can’t reach the goal, output -1 instead.

    样例输入

    3
    1 1 0
    1 0 1
    0 1 1
    3
    2 3 1
    2 2 1
    0 1 0

    样例输出

    2
    3

    题意

    给你个N*N的矩阵,每次操作选择(i,j),然后(i,j)和周围4格都-1,3->2->1->0->3。问最少操作次数。

    题解

    爆搜4^36肯定不行,考虑优化。

    可以发现,如果确定第一行每个位置的变化次数,那么下一行的变化次数也就确定了,最后判断是否每个数都变成0。

    总时间复杂度O(4^6*36)。

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n,a[7][7],c[7][7],b[7],minn;
     4 int dx[]={0,0,1,0,-1};
     5 int dy[]={1,-1,0,0,0};
     6 void dfs(int p,int ans)
     7 {
     8     if(p==n+1)
     9     {
    10         for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)c[i][j]=a[i][j];
    11         for(int j=1;j<=n;j++)
    12             for(int k=0;k<5;k++)
    13             {
    14                 int xx=1+dx[k];
    15                 int yy=j+dy[k];
    16                 if(xx>=1&&xx<=n&&yy>=1&&yy<=n)c[xx][yy]=(c[xx][yy]-b[j]+4)%4;
    17             }
    18         for(int i=2;i<=n;i++)
    19             for(int j=1;j<=n;j++)
    20                 if(c[i-1][j])
    21                 {
    22                     ans+=c[i-1][j];
    23                     for(int k=0;k<5;k++)
    24                     {
    25                         int xx=i+dx[k];
    26                         int yy=j+dy[k];
    27                         if(xx>=1&&xx<=n&&yy>=1&&yy<=n)c[xx][yy]=(c[xx][yy]-c[i-1][j]+4)%4;
    28                     }
    29                 }
    30         for(int i=1;i<=n;i++)
    31             for(int j=1;j<=n;j++)
    32                 if(c[i][j])
    33                     goto e;
    34         minn=min(minn,ans);
    35         e:return;
    36     }
    37     for(int i=0;i<4;i++)
    38     {
    39         b[p]=i;
    40         dfs(p+1,ans+i);
    41     }
    42 }
    43 int main()
    44 {
    45     while(scanf("%d",&n)!=EOF)
    46     {
    47         for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)scanf("%d",&a[i][j]);
    48         minn=1e9;
    49         dfs(1,0);
    50         printf("%d
    ",minn==1e9?-1:minn);
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    vim使用
    linux下编译openjdk8
    《鸟哥的Linux私房菜》学习笔记(9)——条件判断
    《鸟哥的Linux私房菜》学习笔记(8)——bash脚本编程之变量
    《鸟哥的Linux私房菜》学习笔记(7)——grep及正则表达式
    《鸟哥的Linux私房菜》学习笔记(6)——管道及IO重定向
    《鸟哥的Linux私房菜》学习笔记(5)——权限管理
    《鸟哥的Linux私房菜》学习笔记(4)——用户和组
    《鸟哥的Linux私房菜》学习笔记(0)——磁盘与文件系统管理
    Intellij Idea 创建JavaWeb项目
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/11642602.html
Copyright © 2011-2022 走看看