A program test:

You are given N round clocks.

Every clock has M hands, and these hands can point to positions 1, 2, 3, ..., P (yes, these represent numbers around each face). The clocks are represented by the matrix A consisting of N rows and M columns of integers. The first row represents the hands of the first clock, and so on.

For example, you are given matrix A consisting of five rows and two columns, and P = 4:

  A[0][0] = 1    A[0][1] = 2
A[1][0] = 2 A[1][1] = 4
A[2][0] = 4 A[2][1] = 3
A[3][0] = 2 A[3][1] = 3
A[4][0] = 1 A[4][1] = 3

You can rotate the clocks to obtain several clocks that look identical. For example, if you rotate the third, fourth and fifth clocks you can obtain the following clocks:

After rotation, you have four pairs of clocks that look the same: (1, 3), (1, 4), (2, 5) and (3, 4).

type TMatrix = array of array of longint;

Write a function:

int solution(int **A, int N, int M, int P);

int solution(NSMutableArray *A, int P);

int solution(const vector< vector<int> > &A, int P);

class Solution { int solution(int[][] A, int P); }

class Solution { public int solution(int[][] A, int P); }

object Solution { def solution(A: Array[Array[Int]], P: Int): Int }

function solution(A, P);

function solution(A, P)

function solution($A, $P);

function solution(A: TMatrix; N: longint; M: longint; P: longint): longint;

def solution(A, P)

sub solution { my ($A, $P)=@_; my @A=@$A; ... }

def solution(a, p)

Private Function solution ( A As Integer()(), P As Integer ) as Integer

that, given a zero-indexed matrix A consisting of N rows and M columns of integers and integer P, returns the maximal number of pairs of clocks that can look the same after rotation.

For example, given the following array A and P = 4:

    A[0][0] = 1     A[0][1] = 2
A[1][0] = 2 A[1][1] = 4
A[2][0] = 4 A[2][1] = 3
A[3][0] = 2 A[3][1] = 3
A[4][0] = 1 A[4][1] = 3

the function should return 4, as explained above.

Assume that:

  • N is an integer within the range [1..500];
  • M is an integer within the range [1..500];
  • P is an integer within the range [1..1,000,000,000];
  • each element of matrix A is an integer within the range [1..P];
  • the elements of each row of matrix A are all distinct.

Complexity:

  • expected worst-case time complexity is O(N*M*log(M)+N*log(N));
  • expected worst-case space complexity is O(N*M).

Here is my solution:

     class Program
{
static void Main(string[] args)
{
int[][] Testcase = new int[][] { new int[] { , , , }, new int[] { , , , },
new int[] { , , , }, new int[]{ , , , },
new int[]{ , , , }, new int[]{ , , , } };
//result should be 7
Console.WriteLine(new Solution().solution(Testcase, ));
}
} class Solution
{
public int solution(int[][] A, int P)
{
int result = ;
int hands = A[].Length; Dictionary<int[], int> buckets = new Dictionary<int[], int>();
buckets.Add (A[],); //fill the buckets
bool flgFind = false;
foreach(int[] oneClock in A)
{
flgFind = false;
foreach(int[] bucket in buckets.Keys)
{
if (CanbeRotatedToEqual(oneClock, bucket,P) == true)
{
buckets[bucket] += ;
flgFind = true;
break;
}
}
if(flgFind == false)
buckets.Add(oneClock, ); } //calculate the total pairs
foreach (int k in buckets.Values)
result += k * (k - ) / ; return result; } bool CanbeRotatedToEqual(int[] source, int[] target, int P)
{
bool flgJ = false;
int hands = source.Length;
for (int i = ; i < hands; i++)
{
int subValue = target[] - source[i]; flgJ = false;
for (int j = ; j < hands; j++)
{
int newS = source[(i + j) % hands] + subValue;
if (newS <= )
newS += P;
if (newS != target[j])
{
flgJ = true;
break;
}
}
//When flgJ is still false, that means after the source clock rotates subValue steps,
//it is identical with the target one.
if (flgJ == false)
return true;
}
//if this loop end normally, that means the source clock cannot rotate to the target one.
return false;
}
}

2013/8/18: This is not valid version.

rotate the clock的更多相关文章

  1. 使用canvas绘制时钟

    使用canvas绘制时钟  什么使canvas呢?HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图 ...

  2. 教你五步制作精美的HTML时钟

    学了一段时间的HTML.CSS和JS后,给大家做一款漂亮的不像实力派的HTML时钟,先看图:涉及到的知识点有: CSS3动画.DOM操作.定时器.圆点坐标的计算(好多人是不是已经还给自己的老师了~)  ...

  3. [Leetcode][Python]48: Rotate Image

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...

  4. canvas 动画 时钟clock

    <canvas id="clock" width="500" height="500"></canvas> func ...

  5. qt下的时钟程序(简单美丽,继承自QWidget的Clock,用timer调用update刷新,然后使用paintEvent作画就行了,超详细中文注释)good

    最近抽空又看了下qt,发现用它来实现一些东西真的很容易比如下面这个例子,绘制了个圆形的时钟,但代码却清晰易懂[例子源自奇趣科技提供的例子]因为清晰,所以就只写注释了,吼吼其实也就这么几行代码头文件 / ...

  6. Unity项目 - 简单时钟 Clock

    项目展示 Github项目地址:简单时钟 Clock 制作流程 表盘绘制: 采用Aseprite 像素绘图软件绘制表盘及指针.本例钟表素材大小 256x256,存储格式为png,但发现导入Unity后 ...

  7. (转)rotatelogs - Piped logging program to rotate Apache logs

    原文:http://publib.boulder.ibm.com/httpserv/manual60/programs/rotatelogs.html rotatelogs is a simple p ...

  8. CodeChef-----February Challenge 2018---Broken Clock(极坐标+三角函数递推+矩阵快速幂)

    链接:  https://www.codechef.com/FEB18/problems/BROCLK Broken Clock Problem Code: BROCLK Chef has a clo ...

  9. Canvas绘图之平移translate、旋转rotate、缩放scale

    画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...

随机推荐

  1. 关于debug时的一些操作

    当进入一个for循环时,想要看i==49或者其它的行,可以进行如下操作: 在for循环中打断点,点击鼠标右键,选择如下: 在弹出的页面中选择Breakpoint Properties,输入i==49, ...

  2. mysql source命令超大文件导入方法总结

    本文章来给各位朋友介绍利用mysql source命令超大文件导入方法总结,下面收集了两种解决办法,一种是把数据库分文件导出然后再导入,另一种是修改my.ini配置文件,下面我一一给各位朋友介绍. 导 ...

  3. [NOIP2009] 靶形数独(搜索+剪枝)

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了他最近发明的 ...

  4. 【ZJOI2004】嗅探器

    练tarjian不错的题,连WA几次后终于会记住tarjian的模板了 原题: 某军搞信息对抗实战演习.红军成功地侵入了蓝军的内部网络.蓝军共有两个信息中心.红军计划在某台中间服务器上安装一个嗅探器, ...

  5. jQuery的attr与prop,attribute和property区别

    jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...

  6. 鼠标滚动事件兼容性 wheel、onwheel

    wheelEvent = "onwheel" in document.createElement("div") ? "wheel" : // ...

  7. go语言的print

    代码: package main import ( "fmt" ) type point struct { x, y int } func main() { //Go 为常规 Go ...

  8. 【1】第一次电话面试---上海EMC

    时间是2016//11月,投的是上海的EMC2公司的JavaWeb开发岗,第一次接到的电话面试,问的题目很基础基础,很遗憾,本人在掌握的太不好,回答的很乱,目测定挂.下面记下HR问的问题及回答. 首先 ...

  9. Android学习笔记(一)

    活动(Actiity)是一种可以包含用户界面的组件,主要用于和用户进行交互.一个应用中可以包含零个或多个活动. 所有的自己写Activity都继承于Activity类.项目中的任何活动都应该改重写Ac ...

  10. VS2013的一些常用快捷键

    1.回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合键“Ctrl + -”: 2)前进到下一个光标位置:“Ctrl + Shift + - ”. 2.复制/剪切/删除整行代码 ...