Ancient Messages: Difference between revisions
Jump to navigation
Jump to search
imported>Kmk21 Created page with "Category:ICPC Problems Category:Finals2011 Category:Algorithm Easy Category:Implementation Hard Category:BFS Category:Topology" |
imported>Kmk21 No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
Since it says that there are no guarantees about scale or anything, it means that the images have to be topologically equivalent, which for 2D, means they have the same number of "holes" in them. Looking at the pictures, it's clear that this is the case as they all have different numbers of holes | |||
once you've "unpacked" the picture | |||
#find the background by iterating around the entire edge of the picture and flood filling | |||
#iterate through the entire picture. when you find a hole, fill it and increment the count | |||
#lookup which symbol matches the count | |||
<syntaxhighlight line lang="java"> | |||
import java.util.*; | |||
public class c { | |||
Scanner in=new Scanner(System.in); | |||
public static void main(String[] args) { | |||
new c().go(); | |||
} | |||
private void go() { | |||
int cnum=0; | |||
while(true){ | |||
cnum++; | |||
int r=in.nextInt(); | |||
int c=in.nextInt(); | |||
if(r==0&&c==0)return; | |||
in.nextLine(); | |||
int[][] a=new int[r][c*4]; | |||
int[] count=new int[6]; | |||
for(int i=0;i<r;i++){ | |||
String line=in.nextLine(); | |||
for(int j=0;j<a[0].length;j++){ | |||
a[i][j]=(Integer.parseInt(line.substring(j/4,j/4+1),16)>>(3-j%4))&1; | |||
} | |||
} | |||
for(int i=0;i<a[0].length;i++){ | |||
if(a[0][i]==0)flood(a,0,i,-1); | |||
if(a[a.length-1][i]==0)flood(a,a.length-1,i,-1); | |||
} | |||
for(int i=0;i<a.length;i++){ | |||
if(a[i][0]==0)flood(a,i,0,-1); | |||
if(a[i][a[0].length-1]==0)flood(a,i,a[0].length-1,-1); | |||
} | |||
for(int i=0;i<a.length;i++){ | |||
for(int j=0;j<a[0].length;j++){ | |||
if(a[i][j]==1)count[shapeFlood(a,i,j)]++; | |||
} | |||
} | |||
System.out.printf("Case %d: ", cnum); | |||
for(int i=0;i<count[1];i++)System.out.print("A"); | |||
for(int i=0;i<count[5];i++)System.out.print("D"); | |||
for(int i=0;i<count[3];i++)System.out.print("J"); | |||
for(int i=0;i<count[2];i++)System.out.print("K"); | |||
for(int i=0;i<count[4];i++)System.out.print("S"); | |||
for(int i=0;i<count[0];i++)System.out.print("W"); | |||
System.out.println(); | |||
} | |||
} | |||
private int shapeFlood(int[][] a, int i, int j) { | |||
Queue<Integer> q=new LinkedList<Integer>(); | |||
q.offer(i); | |||
q.offer(j); | |||
int holes=0; | |||
while(!q.isEmpty()){ | |||
i=q.poll(); | |||
j=q.poll(); | |||
if(i<0||j<0||i>=a.length||j>=a[0].length)continue; | |||
if(a[i][j]==2)continue; | |||
if(a[i][j]==-1)continue; | |||
if(a[i][j]==0){ | |||
holes++; | |||
flood(a,i,j,2); | |||
} | |||
a[i][j]=2; | |||
q.offer(i); | |||
q.offer(j+1); | |||
q.offer(i); | |||
q.offer(j-1); | |||
q.offer(i+1); | |||
q.offer(j); | |||
q.offer(i-1); | |||
q.offer(j); | |||
} | |||
return holes; | |||
} | |||
private void flood(int[][] a, int i, int j, int k) { | |||
Queue<Integer> q=new LinkedList<Integer>(); | |||
q.offer(i); | |||
q.offer(j); | |||
while(!q.isEmpty()){ | |||
i=q.poll(); | |||
j=q.poll(); | |||
if(i<0||j<0||i>=a.length||j>=a[0].length)continue; | |||
if(a[i][j]!=0)continue; | |||
a[i][j]=k; | |||
q.offer(i); | |||
q.offer(j+1); | |||
q.offer(i); | |||
q.offer(j-1); | |||
q.offer(i+1); | |||
q.offer(j); | |||
q.offer(i-1); | |||
q.offer(j); | |||
} | |||
} | |||
} | |||
//Ancient Messages | |||
//ascii | |||
//topology | |||
//flood fill</syntaxhighlight> | |||
[[Category:ICPC Problems]] | [[Category:ICPC Problems]] | ||
[[Category:Finals2011]] | [[Category:Finals2011]] | ||
[[Category:Algorithm Easy]] | [[Category:Algorithm Easy]] | ||
[[Category:Implementation | [[Category:Implementation Medium]] | ||
[[Category:BFS]] | [[Category:BFS]] | ||
[[Category:Topology]] | [[Category:Topology]] | ||
[[Category:Grid]] |
Latest revision as of 23:29, 25 August 2016
Since it says that there are no guarantees about scale or anything, it means that the images have to be topologically equivalent, which for 2D, means they have the same number of "holes" in them. Looking at the pictures, it's clear that this is the case as they all have different numbers of holes
once you've "unpacked" the picture
- find the background by iterating around the entire edge of the picture and flood filling
- iterate through the entire picture. when you find a hole, fill it and increment the count
- lookup which symbol matches the count
import java.util.*;
public class c {
Scanner in=new Scanner(System.in);
public static void main(String[] args) {
new c().go();
}
private void go() {
int cnum=0;
while(true){
cnum++;
int r=in.nextInt();
int c=in.nextInt();
if(r==0&&c==0)return;
in.nextLine();
int[][] a=new int[r][c*4];
int[] count=new int[6];
for(int i=0;i<r;i++){
String line=in.nextLine();
for(int j=0;j<a[0].length;j++){
a[i][j]=(Integer.parseInt(line.substring(j/4,j/4+1),16)>>(3-j%4))&1;
}
}
for(int i=0;i<a[0].length;i++){
if(a[0][i]==0)flood(a,0,i,-1);
if(a[a.length-1][i]==0)flood(a,a.length-1,i,-1);
}
for(int i=0;i<a.length;i++){
if(a[i][0]==0)flood(a,i,0,-1);
if(a[i][a[0].length-1]==0)flood(a,i,a[0].length-1,-1);
}
for(int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
if(a[i][j]==1)count[shapeFlood(a,i,j)]++;
}
}
System.out.printf("Case %d: ", cnum);
for(int i=0;i<count[1];i++)System.out.print("A");
for(int i=0;i<count[5];i++)System.out.print("D");
for(int i=0;i<count[3];i++)System.out.print("J");
for(int i=0;i<count[2];i++)System.out.print("K");
for(int i=0;i<count[4];i++)System.out.print("S");
for(int i=0;i<count[0];i++)System.out.print("W");
System.out.println();
}
}
private int shapeFlood(int[][] a, int i, int j) {
Queue<Integer> q=new LinkedList<Integer>();
q.offer(i);
q.offer(j);
int holes=0;
while(!q.isEmpty()){
i=q.poll();
j=q.poll();
if(i<0||j<0||i>=a.length||j>=a[0].length)continue;
if(a[i][j]==2)continue;
if(a[i][j]==-1)continue;
if(a[i][j]==0){
holes++;
flood(a,i,j,2);
}
a[i][j]=2;
q.offer(i);
q.offer(j+1);
q.offer(i);
q.offer(j-1);
q.offer(i+1);
q.offer(j);
q.offer(i-1);
q.offer(j);
}
return holes;
}
private void flood(int[][] a, int i, int j, int k) {
Queue<Integer> q=new LinkedList<Integer>();
q.offer(i);
q.offer(j);
while(!q.isEmpty()){
i=q.poll();
j=q.poll();
if(i<0||j<0||i>=a.length||j>=a[0].length)continue;
if(a[i][j]!=0)continue;
a[i][j]=k;
q.offer(i);
q.offer(j+1);
q.offer(i);
q.offer(j-1);
q.offer(i+1);
q.offer(j);
q.offer(i-1);
q.offer(j);
}
}
}
//Ancient Messages
//ascii
//topology
//flood fill