- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在解决扫雷器问题时遇到了麻烦,因为我得到的索引超出范围并且无法弄清楚原因: Here is the link of the problem
我认为我的代码效率不高,但我只想解决这个问题,这里是代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Mine_Sweepers {
public static void main(String [] args) throws IOException
{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
//Taking the dimensions of the grid...
StringTokenizer s=new StringTokenizer(bf.readLine());
int b=Integer.parseInt(s.nextToken()), a=Integer.parseInt(s.nextToken());
while(b!=0 && a!=0)
{
mine(b,a);
s=new StringTokenizer(bf.readLine());
b=Integer.parseInt(s.nextToken());
a=Integer.parseInt(s.nextToken());
}
}
public static void mine(int b,int a) throws IOException
{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
boolean array_first=false,array_last=false,string_first=false,string_last=false;
String []x=new String[a];
for(int i=0;i<a;i++)
x[i]=bf.readLine();
for(int i=0;i<a;i++)
{
//Here I am checking whether this is the first field in the array or if it is the last
array_first=(i==0)?true:false;
array_last=(i==a-1)?true:false;
for(int j=0; j<b; j++)
{
//Here I am checking whether this is the first char in the String in the array or if it is the last
string_first=(j==0)?true:false;
string_last=(j==a-1)?true:false;
int counter=0;
if(x[i].charAt(j)!='*')
{
if(!string_first)
{
if(x[i].charAt(j-1)=='*')
counter++;
if(!array_first & x[i-1].charAt(j-1)=='*')
counter++;
if(!array_last & x[i+1].charAt(j-1)=='*')
counter++;
}
if(!string_last )
{
if(x[i].charAt(j+1)=='*')
counter++;
if(!array_first && x[i-1].charAt(j+1)=='*')
counter++;
if(!array_last & x[i+1].charAt(j+1)=='*')
counter++;
}
if(!array_first & x[i-1].charAt(j)=='*')
counter++;
if(!array_last & x[i+1].charAt(j)=='*')
counter++;
System.out.print(counter);
}
else
System.out.print('*');
}
System.out.println();
}
}
}
最佳答案
您正在使用 &
,does not shortcircuit .
if(!array_first & x[i-1].charAt(j+1)=='*')
所以 !array_first
和 x[i-1].charAt(j+1)=='*'
被求值,无论。您希望第二部分仅在第一部分评估为真时才评估,因此如果 !array_first
为假(因此 array_first
为真),则其余部分不会评估(因为如果是,i-1
将为负数,您将得到一个错误)。为此使用 &&
。不仅在这条线上,而且在任何地方。
如果您使用如下模式:
if <valid indexes> and <access to those indexes>:
do work
您应该始终使用 &&
作为您的逻辑 和
运算符。
关于java - UVa 的扫雷 (10189),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920675/
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
好吧,很难找到答案,因为我不知道如何用英语表达这个问题!所以, 我正在尝试做最长的午睡问题: https://uva.onlinejudge.org/external/101/10191.pdf 我的
我正在尝试了解 UVA 问题 1193 的示例解决方案: 问题陈述: 解决方法: #include #include #include #include #include #include
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
如何将最长公共(public)子序列简化为 O(nlog n) 最长递增子序列问题 10635 uva .我需要一些有关解决问题的逻辑方面的帮助。 最佳答案 对于两个角色之一(假设是公主)的路线的每一
这是实际问题的链接,here 我已多次提交代码,但每次都会出现编译错误。原始消息是 "Main.java:4: error: class Egypt is public, should be decl
我认为这个问题可以通过平方根int数来解决。 为什么此代码显示错误的答案.....? #include #include int main(){ long int n; while(
UVA链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem
所以我试图解决 uva 11959 Dice .但是问题给出的第三个例子给了我错误的答案。然后我发现如果我改变 cin >> d1 >> d2; 到 scanf("%lx %lx", &d1, &d2
注意下面的评论。由于 GCC 中的错误,它无法在 UVA 上编译。 #include #include #include #include #include class Board { pu
这是我第一次使用在线判断,我尝试了一个简单的程序来熟悉环境。 这是 question . 我按照下面的方法解决了,但是得到了一个错误的答案! #include #include #include in
我的代码需要更多测试用例,它显示了正确的答案,但 UVa 仍然不接受它。 指令是,当收到请求时,它们会根据预定的主题列表进行分类。每一位支持人员都负责这些主题中的一个或多个,并且每个主题都分配有一名或
我正在研究 UVa #112 Tree Summing .我有我认为应该可行的解决方案,但由于我对问题的基本误解,它没有被在线法官接受。考虑以下输入: -1 (-1()()) 77 (77(1()()
所有的测试用例我都试过了,但是在线判断还是给我一个错误的答案 https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&pag
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在尝试为 UVa 594 "One Little, Two Little, Three Little Endians" 编写 Java 代码. 长话短说:问题在于从输入流中获取输入并将小端转换为大
我在解决扫雷器问题时遇到了麻烦,因为我得到的索引超出范围并且无法弄清楚原因: Here is the link of the problem 我认为我的代码效率不高,但我只想解决这个问题,这里是代码:
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我正在尝试解决 UVA 417 ,但我无法这样做。我见过的所有解决方案首先生成所有可能的值,将它们存储在映射中,然后搜索以找到所需的字符串。这对我来说似乎很不优雅。没有办法从数学上解决这个问题吗? 考
我是一名优秀的程序员,十分优秀!