(C#)微软校招笔试题Lost in the City
(C#)微软校招笔试题Lost in the City
题目描述
Little Hi gets lost in the city. He does not know where he is. He does not know which is north.
, Little Hi has a map of the city. The map can be as a grid of NM blocks. Each block is by a pair of . The block at the north-west corner is (1, 1) and the one at the south-east corner is (N, M). Each block is by a , the on that block: '.' for empty area, 'P' for parks, 'H' for houses, 'S' for streets, 'M' for malls, 'G' for , 'T' for trees and etc.
Given the blocks of 33 area that Little Hi(Little Hi is at the middle block of the 3*3 area), please find out the of him. Note that Little Hi is , the upper side of the area may be north side, south side, east side or west side.
输入
Line 1: two , N and M(3 a).(.OrderBy(a => a))(此处用到了System.Linq,还不是很了解,是一个要填的大坑)。
代码
写了一个下午,终于AC了orz。水平有限微软笔试题 绳子,多年以后看自己的代码会不会想骂写以下代码的自己哈哈哈。
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
string[] NM = (Console.ReadLine()).Split(' ');
int N = Convert.ToInt32(NM[0]);
int M = Convert.ToInt32(NM[1]);
string[] map = new string[N];
for (int i = 1; i <= N; i++)
{
map[i - 1] = Console.ReadLine();
}
string[] locate = new string[3];
for (int i = 1; i <= 3; i++)
{
locate[i - 1] = Console.ReadLine();
}
string centerPoint = locate[1].Substring(1, 1);
string[] centerAround = {locate[0].Substring(0,1), locate[0].Substring(1, 1), locate[0].Substring(2, 1),
locate[1].Substring(0,1),locate[1].Substring(2,1),
locate[2].Substring(0,1),locate[2].Substring(1,1),locate[2].Substring(2,1)};
string temp;
string[][] allCenterAround = new string[8][];
for (int j = 0; j < centerAround.Length; j++)
{
for (int k = 0; k < centerAround.Length; k++)
{
if (k + 1 < centerAround.Length)
{
temp = centerAround[k];
centerAround[k] = centerAround[k + 1];
centerAround[k + 1] = temp;
}
}
allCenterAround[j] = centerAround;
}
for (int i = 1; i < N - 1; i++)
{
int search = map[i].IndexOf(centerPoint, 1);
while (search <= M - 2 && search != -1)
{
string[] searchArround = {map[i-1].Substring(search-1,1), map[i - 1].Substring(search, 1), map[i - 1].Substring(search + 1, 1),
map[i].Substring(search-1,1),map[i].Substring(search+1,1),
map[i+1].Substring(search-1,1),map[i+1].Substring(search,1),map[i+1].Substring(search+1,1)};
foreach (string[] CenterAroundItem in allCenterAround)
{
if (CenterAroundItem.OrderBy(a => a).SequenceEqual(searchArround.OrderBy(a => a)))
{
Console.WriteLine((i + 1) + " " + (search + 1));
break;
}
}
search = map[i].IndexOf(centerPoint, search + 1);
}
}
}
}
体会
.png
啊啊我居然也感受到了AC的快感!之前没有意识到要刷这些算法题,不过现在为时未晚,重在坚持。
不过C#的效率也是让我大跌眼睛的。一个朋友用C++做这道题目微软笔试题 绳子,和我的算法差不多,但是程序跑的时间和占用内存方面都优于我的(见上图)。嗯……他的程序跑样例的时间为2ms,占用内存为几kb。C++的效率真的不服不行。但我个人还是很享受用C#编程的,感觉语法上更加容易理解1688黄页,而且对程序员更加友好?不过目前的大学课程安排的是学习Java,或许我迟点有空会用Java把这题重新做一遍吧。
昨晚的微软预科生校招笔试,把我虐得怀疑人生。不过受到刺激也不失为一件好事,让我意识到自己在算法上面的短板(C#)微软校招笔试题Lost in the City,希望下一年做的时候能够得心应手一点点吧。
这算起来是我在简书的第一篇博客呢,贵在坚持!
最后编辑于 :2017.12.06 07:12:06
©著作权归作者所有,转载或内容合作请联系作者
【本文来源于互联网转载,如侵犯您的权益或不适传播,请邮件通知我们删除】