我已经编写了一个程序来检查国王可以K步移动的位置数。我有一个大小为8×8的棋盘,行和列的标记为1到8。假设我们的国王在位置1,3;他可以移至5个新位置,并且可能会保留到当前位置,因此总体而言,我们的国王可以移至6个位置。我们的国王可以移动的新地方的有效性可以通过公式Square(r'-r)+Square(c'-c)<=2
来检查,其中r'
和c'
是要检查的牢房的位置。
我的代码对于K = 1和2可以正常工作,但是对于3个或更多的K值,结果开始出现偏差。
import java.util.Scanner;
class Chess {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int testCases;
testCases = input.nextInt();
while (testCases-- > 0 && testCases <= 512) {
int R, C, K, count = 0;
R = input.nextInt();
C = input.nextInt();
K = input.nextInt();
if (R >= 1 && R <= 8 && C <= 8 && C >= 1 && K <= 8 && K >= 1) {
for (double rowIndex = 1; rowIndex <= 8; rowIndex++) {
for (double columnIndex = 1; columnIndex <= 8; columnIndex++) {
if (Math.pow((rowIndex - R), 2) + Math.pow((columnIndex - C), 2) <= (2 * Math.pow(K, 2))) {
count++;
}
}
}
}
System.out.println(count);
}
}
}
答案 0 :(得分:0)
不是100%肯定,但是您确实知道从技术上来说R,C和K从1开始,而count仍为0,对吗?这是因为您在使用它们之前先移到下一个int。
我将代码调整为如下所示,看看效果是否更好!
struct task_struct {
/* these are hardcoded - don't touch */
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
long counter;
long priority;
unsigned long signal;
unsigned long blocked; /* bitmap of masked signals */
unsigned long flags; /* per process flags, defined below */
int errno;
long debugreg[8]; /* Hardware debugging registers */
struct exec_domain *exec_domain;
/* various fields */
struct linux_binfmt *binfmt;
struct task_struct *next_task, *prev_task;
struct task_struct *next_run, *prev_run;
unsigned long saved_kernel_stack;
unsigned long kernel_stack_page;
int exit_code, exit_signal;
/* ??? */
unsigned long personality;
int dumpable:1;
int did_exec:1;
int pid;
int pgrp;
int tty_old_pgrp;
int session;
/* boolean value for session group leader */
int leader;
int groups[NGROUPS];
/*
* pointers to (original) parent process, youngest child, younger sibling,
* older sibling, respectively. (p->father can be replaced with
* p->p_pptr->pid)
*/
struct task_struct *p_opptr, *p_pptr, *p_cptr,
*p_ysptr, *p_osptr;
struct wait_queue *wait_chldexit;
unsigned short uid,euid,suid,fsuid;
unsigned short gid,egid,sgid,fsgid;
unsigned long timeout, policy, rt_priority;
unsigned long it_real_value, it_prof_value, it_virt_value;
unsigned long it_real_incr, it_prof_incr, it_virt_incr;
struct timer_list real_timer;
long utime, stime, cutime, cstime, start_time;
/* mm fault and swap info: this can arguably be seen as either
mm-specific or thread-specific */
unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;
int swappable:1;
unsigned long swap_address;
unsigned long old_maj_flt; /* old value of maj_flt */
unsigned long dec_flt; /* page fault count of the last time */
unsigned long swap_cnt; /* number of pages to swap on next pass */
/* limits */
struct rlimit rlim[RLIM_NLIMITS];
unsigned short used_math;
char comm[16];
/* file system info */
int link_count;
struct tty_struct *tty; /* NULL if no tty */
/* ipc stuff */
struct sem_undo *semundo;
struct sem_queue *semsleeping;
/* ldt for this task - used by Wine. If NULL, default_ldt is used */
struct desc_struct *ldt;
/* tss for this task */
struct thread_struct tss;
/* filesystem information */
struct fs_struct *fs;
/* open file information */
struct files_struct *files;
/* memory management info */
struct mm_struct *mm;
/* signal handlers */
struct signal_struct *sig;
#ifdef __SMP__
int processor;
int last_processor;
int lock_depth; /* Lock depth.
We can context switch in and out
of holding a syscall kernel lock... */
#endif
};
祝你好运!
答案 1 :(得分:0)
您检查新方块的有效性的公式不正确;它不应该涉及平方。如您所见,对于K = 3
,您的情况变为
(r' - r)² + (c' - c)² ≤ 2 × 3² = 18
,实际上,可以使r' = r + 4
和c' = c
满足,因为16≤18。但这意味着国王向上移动了四个方块!
相反,您可以在各个方向上重新声明您的状况:
k
,但最多不超过第8 行,因此国王可以到达的最高行是rmax = min(r + k, 8)
; rmin = max(r - k, 1)
; cmax = min(c + k, 8)
; cmin = max(c - k, 1)
。然后,您可以简单地将答案计算为(rmax - rmin + 1) × (cmax - cmin + 1)
。从直觉上讲,这是有道理的,因为有效区域应该是一个矩形,跨行rmin
至rmax
和列cmin
至cmax
。