Problem in submitting my java code in spoj
I have tried various test cases and everything is working in Eclipse. But while submitting it shows NZEC error.Can anyone please tell me how to rectify this or what is the problem.Its just been three weeks since i started coding and I am finding it difficult to find the errors that occur during runtime.
import java.util.Scanner;
class Main {
public static void main(String args) {
Scanner alpha= new Scanner(System.in);
int n= alpha.nextInt();
int m= alpha.nextInt();
int s1=new int[Math.min(n, m)];
int s2=new int[Math.min(n, m)];
int count=0;
int l= alpha.nextInt();
String maze= new String[n][m];
for(int i=0;i<n;i++) {
char c= alpha.next().toCharArray();
for(int j=0;j<m;j++) {
maze[i][j]=String.valueOf(c[j]);
if(maze[i][j].equals("@")) {
s1[count]=i;
s2[count++]=j;
}
}
}
alpha.close();
boolean flag= false;
int i=0;
while(i<count) {
int entry1=s1[i];
int entry2=s2[i];
i++;
flag= process(entry1,entry2,l,maze,0);
if(flag)
break;
}
System.out.println(flag?"SUCCESS":"IMPOSSIBLE");
}
public static boolean isSafe(int a, int b, Stringmaze) {
return (a<maze.length && b<maze[0].length && !maze[a][b].equals("#"));
}
public static boolean process(int a,int b,int c,Stringmaze,int d){
if(c>=d &&((a+1<maze.length && maze[a+1][b].equals("x"))||(b+1<maze[0].length && maze[a][b+1].equals("x"))||(b-1>=0 && maze[a][b-1].equals("x"))||(a-1>=0 && maze[a-1][b].equals("x"))))
return true;
if(isSafe(a+1,b,maze)) {
if(maze[a+1][b].equals("s"))
{
if( c>0)
return process(a+1,b,c-1,maze,d+1);
else
return false;
}
if(maze[a+1][b].equals("."))
return process(a+1,b,c,maze,d);
}
if(isSafe(a,b+1,maze)) {
if(maze[a][b+1].equals("s"))
{
if( c>0)
return process(a,b+1,c-1,maze,d+1);
else
return false;
}
if(maze[a][b+1].equals("."))
return process(a,b+1,c,maze,d);
}
if(isSafe(a,b-1,maze)) {
if(maze[a][b-1].equals("s"))
{
if( c>0)
return process(a,b-1,c-1,maze,d+1);
else
return false;
}
if(maze[a][b-1].equals("."))
return process(a,b-1,c,maze,d);
}
if(isSafe(a-1,b,maze)) {
if(maze[a-1][b].equals("s"))
{
if( c>0)
return process(a-1,b,c-1,maze,d+1);
else
return false;
}
if(maze[a-1][b].equals("."))
return process(a-1,b,c,maze,d);
}
return false;
}
}
Please refer this following code
the link to the question is https://www.spoj.com/problems/SPIKES/
java
add a comment |
I have tried various test cases and everything is working in Eclipse. But while submitting it shows NZEC error.Can anyone please tell me how to rectify this or what is the problem.Its just been three weeks since i started coding and I am finding it difficult to find the errors that occur during runtime.
import java.util.Scanner;
class Main {
public static void main(String args) {
Scanner alpha= new Scanner(System.in);
int n= alpha.nextInt();
int m= alpha.nextInt();
int s1=new int[Math.min(n, m)];
int s2=new int[Math.min(n, m)];
int count=0;
int l= alpha.nextInt();
String maze= new String[n][m];
for(int i=0;i<n;i++) {
char c= alpha.next().toCharArray();
for(int j=0;j<m;j++) {
maze[i][j]=String.valueOf(c[j]);
if(maze[i][j].equals("@")) {
s1[count]=i;
s2[count++]=j;
}
}
}
alpha.close();
boolean flag= false;
int i=0;
while(i<count) {
int entry1=s1[i];
int entry2=s2[i];
i++;
flag= process(entry1,entry2,l,maze,0);
if(flag)
break;
}
System.out.println(flag?"SUCCESS":"IMPOSSIBLE");
}
public static boolean isSafe(int a, int b, Stringmaze) {
return (a<maze.length && b<maze[0].length && !maze[a][b].equals("#"));
}
public static boolean process(int a,int b,int c,Stringmaze,int d){
if(c>=d &&((a+1<maze.length && maze[a+1][b].equals("x"))||(b+1<maze[0].length && maze[a][b+1].equals("x"))||(b-1>=0 && maze[a][b-1].equals("x"))||(a-1>=0 && maze[a-1][b].equals("x"))))
return true;
if(isSafe(a+1,b,maze)) {
if(maze[a+1][b].equals("s"))
{
if( c>0)
return process(a+1,b,c-1,maze,d+1);
else
return false;
}
if(maze[a+1][b].equals("."))
return process(a+1,b,c,maze,d);
}
if(isSafe(a,b+1,maze)) {
if(maze[a][b+1].equals("s"))
{
if( c>0)
return process(a,b+1,c-1,maze,d+1);
else
return false;
}
if(maze[a][b+1].equals("."))
return process(a,b+1,c,maze,d);
}
if(isSafe(a,b-1,maze)) {
if(maze[a][b-1].equals("s"))
{
if( c>0)
return process(a,b-1,c-1,maze,d+1);
else
return false;
}
if(maze[a][b-1].equals("."))
return process(a,b-1,c,maze,d);
}
if(isSafe(a-1,b,maze)) {
if(maze[a-1][b].equals("s"))
{
if( c>0)
return process(a-1,b,c-1,maze,d+1);
else
return false;
}
if(maze[a-1][b].equals("."))
return process(a-1,b,c,maze,d);
}
return false;
}
}
Please refer this following code
the link to the question is https://www.spoj.com/problems/SPIKES/
java
add a comment |
I have tried various test cases and everything is working in Eclipse. But while submitting it shows NZEC error.Can anyone please tell me how to rectify this or what is the problem.Its just been three weeks since i started coding and I am finding it difficult to find the errors that occur during runtime.
import java.util.Scanner;
class Main {
public static void main(String args) {
Scanner alpha= new Scanner(System.in);
int n= alpha.nextInt();
int m= alpha.nextInt();
int s1=new int[Math.min(n, m)];
int s2=new int[Math.min(n, m)];
int count=0;
int l= alpha.nextInt();
String maze= new String[n][m];
for(int i=0;i<n;i++) {
char c= alpha.next().toCharArray();
for(int j=0;j<m;j++) {
maze[i][j]=String.valueOf(c[j]);
if(maze[i][j].equals("@")) {
s1[count]=i;
s2[count++]=j;
}
}
}
alpha.close();
boolean flag= false;
int i=0;
while(i<count) {
int entry1=s1[i];
int entry2=s2[i];
i++;
flag= process(entry1,entry2,l,maze,0);
if(flag)
break;
}
System.out.println(flag?"SUCCESS":"IMPOSSIBLE");
}
public static boolean isSafe(int a, int b, Stringmaze) {
return (a<maze.length && b<maze[0].length && !maze[a][b].equals("#"));
}
public static boolean process(int a,int b,int c,Stringmaze,int d){
if(c>=d &&((a+1<maze.length && maze[a+1][b].equals("x"))||(b+1<maze[0].length && maze[a][b+1].equals("x"))||(b-1>=0 && maze[a][b-1].equals("x"))||(a-1>=0 && maze[a-1][b].equals("x"))))
return true;
if(isSafe(a+1,b,maze)) {
if(maze[a+1][b].equals("s"))
{
if( c>0)
return process(a+1,b,c-1,maze,d+1);
else
return false;
}
if(maze[a+1][b].equals("."))
return process(a+1,b,c,maze,d);
}
if(isSafe(a,b+1,maze)) {
if(maze[a][b+1].equals("s"))
{
if( c>0)
return process(a,b+1,c-1,maze,d+1);
else
return false;
}
if(maze[a][b+1].equals("."))
return process(a,b+1,c,maze,d);
}
if(isSafe(a,b-1,maze)) {
if(maze[a][b-1].equals("s"))
{
if( c>0)
return process(a,b-1,c-1,maze,d+1);
else
return false;
}
if(maze[a][b-1].equals("."))
return process(a,b-1,c,maze,d);
}
if(isSafe(a-1,b,maze)) {
if(maze[a-1][b].equals("s"))
{
if( c>0)
return process(a-1,b,c-1,maze,d+1);
else
return false;
}
if(maze[a-1][b].equals("."))
return process(a-1,b,c,maze,d);
}
return false;
}
}
Please refer this following code
the link to the question is https://www.spoj.com/problems/SPIKES/
java
I have tried various test cases and everything is working in Eclipse. But while submitting it shows NZEC error.Can anyone please tell me how to rectify this or what is the problem.Its just been three weeks since i started coding and I am finding it difficult to find the errors that occur during runtime.
import java.util.Scanner;
class Main {
public static void main(String args) {
Scanner alpha= new Scanner(System.in);
int n= alpha.nextInt();
int m= alpha.nextInt();
int s1=new int[Math.min(n, m)];
int s2=new int[Math.min(n, m)];
int count=0;
int l= alpha.nextInt();
String maze= new String[n][m];
for(int i=0;i<n;i++) {
char c= alpha.next().toCharArray();
for(int j=0;j<m;j++) {
maze[i][j]=String.valueOf(c[j]);
if(maze[i][j].equals("@")) {
s1[count]=i;
s2[count++]=j;
}
}
}
alpha.close();
boolean flag= false;
int i=0;
while(i<count) {
int entry1=s1[i];
int entry2=s2[i];
i++;
flag= process(entry1,entry2,l,maze,0);
if(flag)
break;
}
System.out.println(flag?"SUCCESS":"IMPOSSIBLE");
}
public static boolean isSafe(int a, int b, Stringmaze) {
return (a<maze.length && b<maze[0].length && !maze[a][b].equals("#"));
}
public static boolean process(int a,int b,int c,Stringmaze,int d){
if(c>=d &&((a+1<maze.length && maze[a+1][b].equals("x"))||(b+1<maze[0].length && maze[a][b+1].equals("x"))||(b-1>=0 && maze[a][b-1].equals("x"))||(a-1>=0 && maze[a-1][b].equals("x"))))
return true;
if(isSafe(a+1,b,maze)) {
if(maze[a+1][b].equals("s"))
{
if( c>0)
return process(a+1,b,c-1,maze,d+1);
else
return false;
}
if(maze[a+1][b].equals("."))
return process(a+1,b,c,maze,d);
}
if(isSafe(a,b+1,maze)) {
if(maze[a][b+1].equals("s"))
{
if( c>0)
return process(a,b+1,c-1,maze,d+1);
else
return false;
}
if(maze[a][b+1].equals("."))
return process(a,b+1,c,maze,d);
}
if(isSafe(a,b-1,maze)) {
if(maze[a][b-1].equals("s"))
{
if( c>0)
return process(a,b-1,c-1,maze,d+1);
else
return false;
}
if(maze[a][b-1].equals("."))
return process(a,b-1,c,maze,d);
}
if(isSafe(a-1,b,maze)) {
if(maze[a-1][b].equals("s"))
{
if( c>0)
return process(a-1,b,c-1,maze,d+1);
else
return false;
}
if(maze[a-1][b].equals("."))
return process(a-1,b,c,maze,d);
}
return false;
}
}
Please refer this following code
the link to the question is https://www.spoj.com/problems/SPIKES/
java
java
edited Nov 20 '18 at 14:10
Michael Lihs
2,51642446
2,51642446
asked Nov 20 '18 at 13:49
Vishwajeet KumarVishwajeet Kumar
12
12
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394480%2fproblem-in-submitting-my-java-code-in-spoj%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394480%2fproblem-in-submitting-my-java-code-in-spoj%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown