前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >程序员面试金典 - 面试题 16.22. 兰顿蚂蚁(deque模拟)

程序员面试金典 - 面试题 16.22. 兰顿蚂蚁(deque模拟)

作者头像
Michael阿明
发布2020-07-13 15:36:21
6870
发布2020-07-13 15:36:21
举报

1. 题目

一只蚂蚁坐在由白色和黑色方格构成的无限网格上。 开始时,网格全白,蚂蚁面向右侧。 每行走一步,蚂蚁执行以下操作。

  • (1) 如果在白色方格上,则翻转方格的颜色,向右(顺时针)转 90 度,并向前移动一个单位。
  • (2) 如果在黑色方格上,则翻转方格的颜色,向左(逆时针方向)转 90 度,并向前移动一个单位。

编写程序来模拟蚂蚁执行的前 K 个动作,并返回最终的网格。

网格由数组表示,每个元素是一个字符串,代表网格中的一行, 黑色方格由 ‘X’ 表示,白色方格由 ‘_’ 表示, 蚂蚁所在的位置由 ‘L’, ‘U’, ‘R’, ‘D’ 表示,分别表示蚂蚁 左、上、右、下 的朝向。 只需要返回能够包含蚂蚁走过的所有方格的最小矩形。

代码语言:javascript
复制
示例 1:
输入: 0
输出: ["R"]

示例 2:
输入: 2
输出:
[
  "_X",
  "LX"
]

示例 3:
输入: 5
输出:
[
  "_U",
  "X_",
  "XX"
]

说明:
K <= 100000

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/langtons-ant-lcci 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

兰顿蚂蚁解释 据说最后会变成这样的图形,有意思!!!

在这里插入图片描述
在这里插入图片描述

2.1 超时解

K = 100000, 超时,string.insert 操作造成数据搬移,性能不高

代码语言:javascript
复制
class Solution {
	vector<string> ans = {"_"};
    					// 右  下  左  上
    vector<char> dirCh = {'R','D','L','U'};
public:
    vector<string> printKMoves(int K) {
        int x = 0, y = 0, d = 0, columns = 1;
        while(K--)
        	update(x,y,d,columns);//更新k步的地图
        ans[x][y] = dirCh[d];//最后的位置填方向
        return ans;
    }

    void update(int& x, int& y, int& d, int& columns) 
    {
    	char ch = ans[x][y];
    	ans[x][y] = (ch=='_' ? 'X' : '_');//反转颜色
    	d = (ch=='_' ? (d+1)%4 : (d+3)%4);//转向
    	if(d==0)//R
    	{
    		y++;
    		if(y == ans[x].size())//需要开新的地图,右侧加列
    		{
    			for(string& s : ans)
    				s += '_';
    			columns++;
    		}
    	}
    	else if(d==1)//D
    	{
    		x++;
    		if(x == ans.size())//需要开新的地图,下面加行
    			ans.push_back(string(columns,'_'));
    	}
    	else if(d==2)//L
    	{
    		y--;
    		if(y<0)//需要开新的地图,左侧加列
    		{
    			y = 0;
    			for(string& s : ans)
    				s = '_'+ s;
    			columns++;
    		}
    	}
    	else//U
    	{
    		x--;
    		if(x<0)//需要开新的地图,上面加行
    		{
    			x = 0;
    			ans.insert(ans.begin(),string(columns,'_'));
    		}
    	}
    }
};

2.2 deque解题

  • 采用双端队列,减少数据搬移操作
代码语言:javascript
复制
class Solution {
	deque<deque<char>> ans = {{'_'}};
    					// 右  下  左  上
    vector<char> dirCh = {'R','D','L','U'};
public:
    vector<string> printKMoves(int K) {
        int x = 0, y = 0, d = 0, columns = 1, i = 0;
        while(K--)
        	update(x,y,d,columns);//更新k步的地图
        ans[x][y] = dirCh[d];//最后的位置填方向
        vector<string> res(ans.size());
        string str;
        for(auto& dq : ans)
        {
            str = "";
            for(char ch : dq)
                str += ch;
            res[i++] = str;
        }
        return res;
    }

    void update(int& x, int& y, int& d, int& columns) 
    {
    	char ch = ans[x][y];
    	ans[x][y] = (ch=='_' ? 'X' : '_');//反转颜色
    	d = (ch=='_' ? (d+1)%4 : (d+3)%4);//转向
    	if(d==0)//R
    	{
    		y++;
    		if(y == ans[x].size())//需要开新的地图,右侧加列
    		{
    			for(auto& s : ans)
    				s.push_back('_');
    			columns++;
    		}
    	}
    	else if(d==1)//D
    	{
    		x++;
    		if(x == ans.size())//需要开新的地图,下面加行
    			ans.push_back(deque<char>(columns,'_'));
    	}
    	else if(d==2)//L
    	{
    		y--;
    		if(y<0)//需要开新的地图,左侧加列
    		{
    			y = 0;
    			for(auto& s : ans)
    				s.push_front('_');
    			columns++;
    		}
    	}
    	else//U
    	{
    		x--;
    		if(x<0)//需要开新的地图,上面加行
    		{
    			x = 0;
    			ans.push_front(deque<char>(columns,'_'));
    		}
    	}
    }
};

516 ms 49.8 MB

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目
  • 2. 解题
    • 2.1 超时解
      • 2.2 deque解题
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档