TopCoder--镜子反射路径问题.

原创 2006年06月15日 16:18:00

矩阵中' ` ' 左斜的镜子\,  ' / ' 为右斜的镜子/. 光路是 - | 和 + .自己运行一下看看吧!

package topcoder;

/*
 * Created on 2005-12-1
 * @author Admin.zhouyuan
 */

public class MirrorPath
{
    private int direction; // 向下走为0,向右走为1,向上走为2, 向左走为3
    private int row;
    private int col;

    public String[] path(String[] map)
    {
        findExit(map);
        char[][] ch = new char[map.length][map[0].length()];
        for (int i = map.length - 1; i >= 0; i--)
            for (int j = map[0].length() - 1; j >= 0; j--)
            {
                ch[i][j] = map[i].charAt(j);
            }
        System.out.println("row = " + this.row + "; col = " + this.col + "; direction = " + direction);
        while (row >= 0 && row < map.length && col >= 0 && col < map[0].length())
        {
            switch (this.direction)
            {
                case 0:
                    switch (ch[row][col])
                    {
                        case '.':
                            ch[row][col] = '|';
                            ++row;
                            break;
                        case '-':
                            ch[row][col] = '+';
                            ++row;
                            break;
                        case '`':
                            direction = 1;
                            ++col;
                            break;
                        case '/':
                            direction = 3;
                            --col;
                            break;
                    }
                    // System.out.println("0");
                    break;

                case 1:
                    switch (ch[row][col])
                    {
                        case '.':
                            ch[row][col] = '-';
                            ++col;
                            break;
                        case '|':
                            ch[row][col] = '+';
                            ++col;
                            break;
                        case '`':
                            direction = 0;
                            ++row;
                            break;
                        case '/':
                            direction = 2;
                            --row;
                            break;
                    }
                    // System.out.println("1");
                    break;

                case 2:
                    switch (ch[row][col])
                    {
                        case '-':
                            ch[row][col] = '+';
                            --row;
                            break;
                        case '.':
                            ch[row][col] = '|';
                            --row;
                            break;
                        case '`':
                            direction = 3;
                            --col;
                            break;
                        case '/':
                            direction = 1;
                            ++col;
                            break;
                    }
                    // System.out.println("2");
                    break;

                case 3:
                    switch (ch[row][col])
                    {
                        case '|':
                            ch[row][col] = '+';
                            --col;
                            break;
                        case '.':
                            ch[row][col] = '-';
                            --col;
                            break;
                        case '`':
                            direction = 2;
                            --row;
                            break;
                        case '/':
                            direction = 0;
                            ++row;
                            break;
                    }
                    break;
            }
        }
        for (int i = map.length - 1; i >= 0; i--)
            map[i] = new String(ch[i]);
        return map;
    }

    private void findExit(String[] map)
    {
        int i = map.length - 1;
        int j = map[0].length() - 1;
        for (; i > 0; i--)
        {
            if (map[i].charAt(0) != '#')
            {
                this.direction = 1;
                this.row = i;
                this.col = 0;
                return;
            }
            else
                if (map[i].charAt(j) != '#')
                {
                    this.direction = 3;
                    this.row = i;
                    this.col = j;
                    return;
                }
        }

        i = map.length - 1;
        j = map[0].length() - 1;
        for (; j >= 0; j--)
        {
            if (map[0].charAt(j) != '#')
            {
                this.direction = 0;
                this.row = 0;
                this.col = j;
                return;
            }
            else
                if (map[map.length - 1].charAt(j) != '#')
                {
                    this.direction = 2;
                    this.col = j;
                    this.row = i;
                    return;
                }
        }

        // System.out.println("direction = " + this.direction);
        return;
    }

    public static void main(String[] args)
    {
        MirrorPath mp = new MirrorPath();
        /*
         * String[] str = new String[]{ "############", "#######/....", "######//####", "#####//#####", "####//######",
         * "..../#######", "############" };
         *
         * String[] str = new String[]{ "###########.#", "#/........./.", "#.#########.#", "#`........./#",
         * "#############" };
         *
         * String[] str = new String[]{ "########.##", "#./......`#", "#../.`....#", "#.`...../.#", "#....`.../#",
         * "###.#######" };
         */
        String[] str = new String[] { "##.########", "#.........#", "..`.`.....#", "#..`......#", "#.`.`.`...#", "#....`....#",
                "#...`.`.`.#", "#.........#", "#.....`./.#", "#.........#", "###########" };
        for (int i = 0; i < str.length; i++)
        {
            System.out.println(str[i]);
        }
        mp.path(str);
        for (int i = 0; i < str.length; i++)
        {
            System.out.println(str[i]);
        }
    }
}

相关文章推荐

Unity 镜子反射特效

最近项目中要用到镜子的反射特效,镜子反射由物理学知识可以知道,镜子中的景物是实际物体的对称表现。由此就衍生了俩种 思路,第一种是讲物体实实在在渲染到镜子之后,第二种是生成一个摄像机使其关于镜子对称,将...

infopath模板路径及注册问题

  • 2016年12月12日 15:08
  • 255B
  • 下载

数据与结构最短路径问题

  • 2015年06月03日 00:21
  • 2KB
  • 下载

【Android基础】-调用前置摄像头实现的‘手机镜子’

前言:镜子是每个爱美的人必备的物品,但是并不是什么时候都带在身边;手机也是每个人必备的物品,而且随身携带,甚至连上厕所都不愿放下;那么是不是可以想办法让手机具有镜子的功能呢?今天我们就来通过调用摄像头...
  • tailyou
  • tailyou
  • 2015年09月02日 19:51
  • 1465

最短路径问题

  • 2017年06月22日 16:51
  • 4KB
  • 下载

如何制作一面智能镜子

MirrorOnTheWall前言创意来源:HomeMirror 原文提到了一个东西,tow-way mirror(双向镜)。这个创意就是利用了单向镜(双向镜)光线强的一面无法看到光线弱的一面这个原...
  • sgwhp
  • sgwhp
  • 2015年10月01日 23:34
  • 2611

多段图的最短路径问题

  • 2013年11月11日 11:10
  • 123KB
  • 下载

最短路径问题

  • 2012年10月10日 20:53
  • 334KB
  • 下载

【D3D11游戏编程】学习笔记十八:模板缓冲区的使用、镜子的实现--模板缓冲实现原理和机制

(注:【D3D11游戏编程】学习笔记系列由CSDN作者BonChoix所写,转载请注明出处:http://blog.csdn.net/BonChoix,谢谢~)         模板缓冲区(Ste...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:TopCoder--镜子反射路径问题.
举报原因:
原因补充:

(最多只允许输入30个字)