Page 1 of 2

### 11953 - Battleships

Posted: Sat Apr 16, 2011 3:52 pm

Code: Select all

``omg thank you so much! super silly mistakes...``
Anyone has any critical test cases? I'm running okay on the sample input, but WA.. my code shouldn't be wrong??

### Re: 11953 - Battleships

Posted: Sun Apr 17, 2011 12:32 am
Hi truewt,

I think, your output is not correct for sample input, you forgot print end of line character, so your output is only one line...

Change

Code: Select all

``````cout << "Case " << counter++ << ": " << ans;
``````
to

Code: Select all

``````cout << "Case " << counter++ << ": " << ans << "\n";
``````
and I hope you will get AC now. Your floodfill seems OK. Don't forget delete your code, when you will have AC

### Re: 11953 - Battleships

Posted: Sun Apr 17, 2011 8:38 am
Thanks so much for the help!!!

### 11953 - Battleships(WA)

Posted: Thu Apr 21, 2011 8:32 pm
Any one has any critical test cases?
I did not find why my code goes for WA.I think my floodfill is ok

Code: Select all

``Removed After ACC``

### Re: 11953 - Battleships

Posted: Sat Jun 11, 2011 8:40 pm
I also got WA. But why???

Code: Select all

``````#include<stdio.h>
long B[109][109],n,cas,cas1,count,i,j;
char A[109][109];

void make(long x,long y,long z,long z1)
{
B[x][y]=1;

if(z+1<=n/2)
{
if((z1==0 ||z1==1)&&x-1>=0&&('x'==A[x-1][y]||'@'==A[x-1][y])&&B[x-1][y]==0)
make(x-1,y,z+1,1);
else if((z1==0 ||z1==1)&&x+1<n&&('x'==A[x+1][y]||'@'==A[x+1][y])&&B[x+1][y]==0)
make(x+1,y,z+1,1);
else if((z1==0 ||z1==2)&&y-1>=0&&('x'==A[x][y-1]||'@'==A[x][y-1])&&B[x][y-1]==0)
make(x,y-1,z+1,2);
else if((z1==0 ||z1==2)&&y+1<n&&('x'==A[x][y+1]||'@'==A[x][y+1])&&B[x][y+1]==0)
make(x,y+1,z+1,2);
}

}

int main()
{

scanf("%ld",&cas);

for(cas1=1;cas1<=cas;cas1++)
{
scanf("%ld",&n);

for(i=0;i<n;i++)
{
scanf("%s",A[i]);
for(j=0;j<n;j++)
B[i][j]=0;
}

count = 0;

for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(A[i][j]=='x'&&B[i][j]==0)
{
count++;
make(i,j,1,0);
}

printf("Case %ld: %ld\n",cas1,count);

}

return 0;
}
``````

### Re: 11953 - Battleships

Posted: Sat Jul 14, 2012 12:49 am
can't find why getting WA. and the problem specification was not too clear to me. please someone help finding the bug

Code: Select all

``````AC
``````

### Re: 11953 - Battleships

Posted: Fri Jul 19, 2013 6:21 pm
Hi
Why Runtime Error?
Thank you so much...

Code: Select all

`` Deleted After AC``

### Re: 11953 - Battleships

Posted: Fri Jul 19, 2013 9:54 pm
From uhunt:
AKJ88> @faraa In your dfs function y+1 can exceed grid boundary. Check if it's within boundary or not, and also 'Case' has capital C.

### Re: 11953 - Battleships

Posted: Sat Jul 20, 2013 12:41 am
@brianfry713
Thank you so much.But I tried many test cases and It doesn't have any problem.
Please give me some test case
Tnx

### Re: 11953 - Battleships

Posted: Sat Jul 20, 2013 9:58 am
try input

Code: Select all

``````1
3
x..
...
@.@
``````

### Re: 11953 - Battleships

Posted: Sat Jul 20, 2013 2:39 pm
Hi.
I know what is the problem and where is it...
But I can't fix it!!!
I tried so much...
Can you help me?
(It takes me wrong answer because if the length of the ships further more n/2.It sometimes don't understand!!!!)

Code: Select all

``````Deleted After Ac :D
``````

### Re: 11953 - Battleships

Posted: Sat Jul 20, 2013 5:33 pm
Use class Main

### Re: 11953 - Battleships

Posted: Sun Jul 21, 2013 12:03 pm
Try input:

Code: Select all

``````1
5
x....
x....
.....
.....
.....``````

### Re: 11953 - Battleships

Posted: Sun Jul 21, 2013 11:26 pm

I tried to write my code very simple...
But time limit...!!!

class Main{

static int[][] ar = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
static char[] temp;
static char[][] grid;
static int cnt;
static boolean alive;

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int t;
t = in.nextInt();

for (int k = 0; k < t; k++) {
int n = in.nextInt();
grid = new char[n + 2][n + 2];
cnt = 0;
in.nextLine();

for (int i = 0; i < n; i++) {
String line = in.nextLine();
temp = line.toCharArray();
for(int j=0;j<temp.length;j++){
grid[i+1][j+1]=temp[j];
}
}

for (int i = 1; i < grid.length - 1; i++) {
for (int j = 1; j < grid.length - 1; j++) {
if (grid[j] == 'x') {
cnt++;
dfs(i, j);
}
}
}

int ans = k + 1;
System.out.println("Case " + ans + ": " + cnt);
}
}

public static void dfs(int x, int y) {
if (x == (grid.length - 1) || y == (grid.length - 1) || x == 0 || y == 0 || grid[x][y] == '.') {
return;
}

grid[x][y] = '.';

for (int i = 0; i < ar.length; i++) {
int r = x + ar[0];
int c = x + ar[1];
dfs(r, c);

}
}
}

### Re: 11953 - Battleships

Posted: Mon Jul 22, 2013 1:05 am

Code: Select all

``````for (int i = 0; i < ar.length; i++)
{
int r = x + ar[i][0];
int c = x + ar[i][1];
dfs(r, c);
}
``````
It's should be

Code: Select all

``````for (int i = 0; i < ar.length; i++)
{
int r = x + ar[i][0];
int c = y + ar[i][1];
dfs(r, c);
}
``````
Am i right ?