zoukankan      html  css  js  c++  java
  • hdu 3500 DFS(限定)

    Fling

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 455    Accepted Submission(s): 190


    Problem Description
    Fling is a kind of puzzle games available on phone.
    This game is played on a board with 7 rows and 8 columns. Each puzzle consists of a set of furballs placed on the board. To solved a puzzle, you need to remove the furballs from board until there is no more than one furball on the board. You do this by ´flinging´ furballs into other furballs, to knock them off the board. You can fling any furballs in four directions (up, left, right, down). The flung furball stops at the front grid of another one as soon as knocking it. And the knocked furball continues to rolling in the same direction until the last knocked one goes off the board. For instance, A furball at (0, 0) rolls right to the furball at (0, 5), then it will stop at (0, 4). Moreover, the latter will roll to right. You cannot fling a furball into a neighbouring furball, the one next to in any of four directions. However, it is permitted for a rolling ball knocks into a ball with a neighbour in that direction.


     
    Input
    The input contains multiple test cases.
    For each case, the 7 lines with 8 characters describe the board. ´X´ represents a empty grid and ´O´ represents a grid with a furball in it. There are no more than 12 furballs in any board.
    Each case separated by a blank line.

     
    Output
    For each case, print a line formatted as "CASE #NUM:", where NUM is the number of current case.
    Then every ´fling´ prints a line. Each line contains two integers X, Y and a character Z. The flung furball is located at grid (X, Y), the top-left grid is (0, 0). And Z represents the direction this furball towards: U (Up), L (Left), R (Right) and D (Down);
    Print a blank line between two cases.
    You can assume that every puzzle could be solved.
    If there are multiple solve sequences, print the smallest one. That is, Two sequences A (A1, A2, A3 ... An) and B (B1, B2, B3 ... Bn). Let k be the smallest number that Ak != Bk.
    Define A < B :
    (1) X in Ak < X in Bk;
    (2) Y in Ak < Y in Bk and X in Ak = X in Bk;
    (3) Z in Ak < Z in Bk and (X,Y) in Ak = (X,Y) in Bk;
    The order of Z: U < L < R < D.

     
    Sample Input
    XXXXXXXX XXOXXXXX XXXXXXXX XXXXXXXX XOXXXXOX XXXXXXXX XXXXXXXX XXXXXXXX XOXOXOOX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
    Sample Output
    CASE #1:
    4 6 L
    1 2 D
    CASE #2:
    1 1 R
    1 4 L
    1 3 R
     
    给出在一个7*8方块上的很多毛球,允许的操作只有用一个毛球取弹另一个毛球,最终的状态是只剩下一个球
    其中:
    不能直接把毛球弹出去
    假如相邻也是一个毛球,则不能往这个方向弹(可理解为无法蓄力)。
    使用dfs()进行搜索,对一个球弹或者不弹进行搜索,记住要使用一个空地图来存储决策前的地图,以便在搜索方向出错是进行回退
     
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    struct node
    {
        int x,y,z;
    }ans[60];
    char mat[10][10];
    char direc[]={'U','L','R','D'};
    int nx[]={-1, 0,  0, 1};
    int ny[]={ 0,-1,  1, 0};
    void fling(int x,int y,int k)
    {
        int flag=0;
        int nxtx=x+nx[k],nxty=y+ny[k];
        if(nxtx<0||nxtx>=7||nxty<0||nxty>=8) {mat[x][y]='X';return;}
        while(mat[nxtx][nxty]=='X')
        {
            nxtx+=nx[k];nxty+=ny[k];
            if(nxtx<0||nxtx>=7||nxty<0||nxty>=8) {flag=1;break;}
        }
        if(flag) {mat[x][y]='X';return;}
        mat[x][y]='X';
        mat[nxtx-nx[k]][nxty-ny[k]]='O';
        fling(nxtx,nxty,k);
    }
    
    bool dfs(int sum,int cnt)
    {
        if(sum==1)
        {
            return 1;
        }
        int nxtx,nxty;
        char t_mat[10][10];
        memcpy(t_mat,mat,sizeof(mat));
        for(int i=0;i<7;i++)
        {
            for(int j=0;j<8;j++)
            {
                if(mat[i][j]!='O') continue;
                for(int k=0;k<4;k++)
                {
                    nxtx=i+nx[k];nxty=j+ny[k];
                    if(nxtx<0||nxtx>=7||nxty<0||nxty>=8) continue;
                    if(mat[nxtx][nxty]=='O') continue;
                    int flag=0;
                    while(mat[nxtx][nxty]=='X')
                    {
                        nxtx+=nx[k];nxty+=ny[k];
                        if(nxtx<0||nxtx>=7||nxty<0||nxty>=8)
                        {
                            flag=1;break;
                        }
                    }
                    if(flag) continue;
                    mat[i][j]='X';
                    mat[nxtx-nx[k]][nxty-ny[k]]='O';
                    fling(nxtx,nxty,k);
                    ans[cnt].x=i;
                    ans[cnt].y=j;
                    ans[cnt].z=k;
                    if(dfs(sum-1,cnt+1)) return 1;
                    memcpy(mat,t_mat,sizeof(t_mat));
                }
            }
        }
        return 0;
    
    }
    int main()
    {
        int ca=1;
        while(scanf("%s",mat[0])!=EOF)
        {
            int sum=0;
            for(int i=1;i<7;i++) scanf("%s",mat[i]);
            for(int i=0;i<7;i++)
            {
                for(int j=0;j<8;j++)
                {
                    if(mat[i][j]=='O')
                    {
                        sum++;
                    }
                }
            }
            memset(ans,-1,sizeof(ans));
            dfs(sum,0);
            if(ca!=1) puts("");
            printf("CASE #%d:
    ",ca++);
            for(int i=0;ans[i].x!=-1;i++)
            {
                printf("%d %d %c
    ",ans[i].x,ans[i].y,direc[ans[i].z]);
            }
        }
        return 0;
    }
  • 相关阅读:
    重读SQLServer技术内幕 -- 故障检测概要
    Android Studio调试React Native项目
    Android 异常处理
    第一天
    记一次"未将对象引用设置到对象的实例"问题的排查过程
    Hyper-V连接虚拟机异常,“无法进行连接,因为可能无法将凭据发送到远程计算机”
    关于TFS2012无法发送警报邮件的问题
    BaseAdapter日常的封装
    android自定义拍照
    Android系统拍照源码
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/5356546.html
Copyright © 2011-2022 走看看