一只蚂蚁坐在由白色和黑色方格构成的无限网格上。 编写程序来模拟蚂蚁执行的前 K 个动作,并返回最终的网格。 网格由数组表示,每个元素是一个字符串,代表网格中的一行, 来源:力扣(LeetCode) 兰顿蚂蚁解释 K = 100000, 超时, 516 ms 49.8 MB
1. 题目
开始时,网格全白,蚂蚁面向右侧。
每行走一步,蚂蚁执行以下操作。
黑色方格由 ‘X’ 表示,白色方格由 ‘_’ 表示,
蚂蚁所在的位置由 ‘L’, ‘U’, ‘R’, ‘D’ 表示,分别表示蚂蚁 左、上、右、下 的朝向。
只需要返回能够包含蚂蚁走过的所有方格的最小矩形。示例 1: 输入: 0 输出: ["R"] 示例 2: 输入: 2 输出: [ "_X", "LX" ] 示例 3: 输入: 5 输出: [ "_U", "X_", "XX" ] 说明: K <= 100000
链接:https://leetcode-cn.com/problems/langtons-ant-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。2. 解题
据说最后会变成这样的图形,有意思!!!
2.1 超时解
string.insert
操作造成数据搬移,性能不高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解题
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,'_')); } } } };
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算