HDOJ 1391 Number Steps(打表DP)

举报
谙忆 发表于 2021/05/28 07:10:49 2021/05/28
【摘要】 Problem Description Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,… as shown in the figure. For example, 1, 2, and 3 has been written at points...

Problem Description
Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,… as shown in the figure. For example, 1, 2, and 3 has been written at points (1,1), (2,0), and (3, 1) respectively and this pattern has continued.

You are to write a program that reads the coordinates of a point (x, y), and writes the number (if any) that has been written at that point. (x, y) coordinates in the input are in the range 0…5000.

Input
The first line of the input is N, the number of test cases for this problem. In each of the N following lines, there is x, and y representing the coordinates (x, y) of a point.

Output
For each point in the input, write the number written at that point or write No Number if there is none.

Sample Input
3
4 2
6 6
3 4

Sample Output
6
12
No Number

不能直接开[5005][5005]的数组,这样内存不够。
因为大部分数据没用,没列只有2个有效数据,所以开[5005][2]就可以了。

import java.util.Scanner;

public class Main{
 static int[][] db = new int[5005][2];
 public static void main(String[] args) {
 dabiao();
 Scanner sc = new Scanner(System.in);
 int t = sc.nextInt();
 while(t-->0){
 int x = sc.nextInt();
 int y = sc.nextInt();
 if(x==0&&y==0){
 System.out.println(0);
 continue;
 }

 if(x==1&&y!=1){
 System.out.println("No Number");
 continue;
 }

 if(x==1&&y==1){
 System.out.println(1);
 continue;
 }

 if(x==y||x==(y+2)){
 if(x==(y+2)){
 System.out.println(db[x][0]);
 }else{
 System.out.println(db[x][1]);
 }
 }else{
 System.out.println("No Number");
 }
 }
 }

 private static void dabiao() {
 int num=2;
 db[0][0]=0;
 boolean is = true;
 for(int i=2;i<=5003;i++){
 if(is){
 db[i][0]=num;
 num++;
 db[i+1][0]=num;
 num++;
 is=!is;
 }else if(!is){
 db[i-1][1]=num;
 num++;
 db[i][1]=num;
 num++;
 is=!is;
 }
 }
// System.out.println(db[2][0]);
// System.out.println(db[2][1]);
// System.out.println(db[3][0]);
// System.out.println(db[3][1]);
// System.out.println(db[4][0]);
// System.out.println(db[4][1]);
// System.out.println(db[5][0]);
// System.out.println(db[5][1]);
 }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。

原文链接:chenhx.blog.csdn.net/article/details/51105667

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。