who can prove it if the amount of baffles that should be opened is equal to N - 2, it will be not enough, because graph will be not connected, and that's why not all cockcroaches will reach the floodgate. But you can build the spanning tree of this graph. It has exactly N - 1 edges, and graph will be connected and now all cockroaches will reach the floodgate. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Scanner; public class Test { public static void main(String[] args) throws IOException { new Test().solve(); } private static Scanner s = new Scanner(System.in); static int n = 0; void solve() throws IOException { String input = null; String[] strArray = new String[30]; while (true) { input = getString(); if (input.equals("#")) { break; } else { saveItToArray(strArray, input); } } checkOutIdentical(strArray); System.out.println(n - 1); } private static String getString() throws IOException {
String input; input = s.nextLine(); return (input); } void saveItToArray(String[] strArray, String input) { boolean isDash = false; for (int i = 0; i < input.length(); i++) { if (input.charAt(i) == '-') { strArray[n++] = input.substring(0, i); strArray[n++] = input.substring(i + 1, input.length()); isDash = true; break; } else { isDash = false; } } if (!isDash) { strArray[n++] = input; } } void checkOutIdentical(String[] strArray) { int count = 0; count = n; for (int i = 1; i < count; i++) { for (int j = i - 1; j >= 0; j--) { if (!strArray[i].equals(" ") && !strArray[j].equals(" ") && strArray[i].equals(strArray[j])) { strArray[i] = " "; n--; } } } } } thanks a lot. I really didn't know what to do with test 13 Cockroaches!蟑螂 Time Limit: 1 second Memory Limit: 1000K It's well-known that the most tenacious of life species on the Earth are cockroaches. They live everywhere if only there in food. And as far as they are unpretentious in food you can find them absolutely everywhere. 众所周知地球上最顽强的生命种类是蟑螂,他们生活在有食物的任何地方。只要有食物,你就可以发现他们的影踪。 A little Lyosha studies at school on a Space station. During one of the school competitions his class has reached the final. A task of the final contest is to exterminate all the cockroaches in the cargo module within minimal time. Lyosha 在太空站的学校学习。在学校的一项竞赛里,他们班已经到达接近尾声,其中一项任务是在最小的时间里消灭货物仓里的蟑螂。 Within the long history of the competitions a unified tactics was worked out. The tactics is as follows: a poison gas is let in one of the module compartments and after that the baffle that separates the compartment from one of the adjacent ones is opened. Cockroaches can't stand the smell of the gas and run to the other compartment. When there's no cockroaches in the treated compartment the baffle is closed. Afterwards analogously the next compartment is treated, and so on. The goal is to move all the cockroaches to the floodgate of the cargo module. Then the outward door is opened and all the cockroaches are engulfed by an open Space. 他们在长时间的竞赛里,已经形成了统一的战术。 战术是这样的:把毒气放在一个厢房里,另外,打开闸门(隔开相邻厢房的门)。蟑螂不能忍受毒气的味道,会纷纷逃向隔壁的已打开的厢房,如此下去。目标是把所有的蟑螂赶向floodgate货物仓(的闸门)。 Lyosha is responsible for programming the control board of the baffles in his team. The baffles are opened slowly, so it's very important to make do with minimal number of baffle openings in order to win in the contest. Your task is to help Lyosha to compute this number. Lyosha负责闸门的规划控制,闸门开得慢,所以这项工作对于竞赛的胜负是很重要的,要设法使到开最少的门。你的任务是帮助Lyosha 计算这个数。 Input The first line contains a name of the floodgate compartment. Each of the next lines contains description of one of the baffles - the names of two compartments separated with a dash (-). The last line contains the only symbol "#". There are cockroaches in all the compartments of the module at first. It's possible to get to the floodgate from every compartment of the module passing several baffles. The total number of compartments doesn't exceed 30. The name of a compartment consists of no more than 20 Latin letters and digits. The large and the small letters should be distinguished. 首行为floodgate厢房名。以下每一行为两个厢房间的闸门名,两个厢房间用(-)隔开。最后一行只有“#”结束。开始时每个厢房都有蟑螂。每个厢房的蟑螂经过几个闸门后都能到达floodgate。厢房的数量不超过30个。厢房名不超过20个字母数字,区分大小写。 Output Your program is to output the only number - the minimal amount of baffles that should be opened (and then closed) in order to move all the cockroaches to the floodgate. 输出最小的数字,即为了能把蟑螂赶到指定的floodgate,要开的门数 (当然开完后又关上) Sample Input Gateway Machinery-Gateway Machinery-Control Control-Central Control-Engine Central-Engine Storage-Gateway Storage-Waste Central-Waste # Sample Output 6 thx anyway for translating it to Chinese Thank, I have Got WA13) now AC I just want to know why the answer is Number_comparments - 1 Because you can make a tree from comparments and then open the baffles from the leaves to the root of tree using only n-1 baffle. Name of a compartment might consists digits. When the number of compartments is 0 you should output 0 (not -1),maybe you guessed why i said "not -1",because when you output (Number_of_Compartments-1) the answer will be -1 because Number_of_Compartments=0 ,maybe it will help you. Good Luck! I used hash which have pased all tests I have got WA6 several times here is my program program Project2; {$APPTYPE CONSOLE} const s=['a'..'z','A'..'Z','0'..'9']; var name:array[1..10000000] of boolean; c,n,i,j:longint; ch:char; str:string; begin readln(str); read(ch); if ch='#' then begin writeln('0'); exit; end; while ch <> '#' do begin c:=100*ord(ch); j:=0; while (ch in s)and(ch<>'#') do begin c:=c+ord(ch); read(ch); inc(j); end; name[c]:=true; while (not (ch in s))and(ch <> '#') do read(ch); end; j:=0; for i:=1 to 10000000 do if name[i] then inc(j); writeln(j-1); readln; readln; end. Only delete readln; readln; And all will be OK! Edited by author 19.02.2009 01:09 Please help me !!!! What's wrong in my code ???????????????????????? var a:array[1..100,1..100]of integer; nn:array[1..100]of string; s,s1:string; kol:integer; w:integer; function find(s:string):integer; var i:integer; begin for i:=1 to kol do if nn[i]=s then begin find:=i; exit; end; find:=kol+1; end; procedure init; var k:integer; ch1,ch2,pp:integer; begin kol:=1; {assign(input,'input.txt'); reset(input);} readln(s); nn[kol]:=s; readln(s); while s<>'#' do begin k:=pos('-',s); s1:=copy(s,1,k-1); delete(s,1,k); pp:=find(s1); if pp> kol then kol:=pp; nn[pp]:=s1; ch1:=pp; pp:=find(s); if pp> kol then kol:=pp; nn[pp]:=s; ch2:=pp; a[ch1,ch2]:=1; a[ch2,ch1]:=1; readln(s); end; {close(input);} end; procedure solve; var i,k,l,p:integer; ss,sp:set of 1..200; min,t,j:integer; ok:boolean; begin ss:=[1..kol];sp:=[]; for i:=1 to kol do if a[1,i]<>0 then begin p:=i; break; end; w:=1; ss:=ss-[1,p];sp:=[1,p]; while ss<>[] do begin for i:=1 to kol do begin ok:=false; if not(i in sp)then for j:=1 to kol do if (j in sp)and(a[i,j]<>0)then begin ss:=ss-[i,j]; sp:=sp+[i,j]; inc(w); break; ok:=true; end; if ok then break; end; end; end; procedure outt; begin writeln(w); end; begin init; solve; outt; end. > Please help me !!!! > What's wrong in my code ???????????????????????? > > var a:array[1..100,1..100]of integer; > nn:array[1..100]of string; > s,s1:string; > kol:integer; > w:integer; > function find(s:string):integer; > var i:integer; > begin > for i:=1 to kol do > if nn[i]=s then > begin > find:=i; > exit; > end; > find:=kol+1; > end; > procedure init; > var k:integer; > ch1,ch2,pp:integer; > begin > kol:=1; > {assign(input,'input.txt'); > reset(input);} > readln(s); > nn[kol]:=s; > readln(s); > while s<>'#' do > begin > k:=pos('-',s); > s1:=copy(s,1,k-1); > delete(s,1,k); > pp:=find(s1); > if pp> kol then kol:=pp; > nn[pp]:=s1; > ch1:=pp; > pp:=find(s); > if pp> kol then kol:=pp; > nn[pp]:=s; > ch2:=pp; > a[ch1,ch2]:=1; > a[ch2,ch1]:=1; > readln(s); > end; > {close(input);} > end; > procedure solve; > var i,k,l,p:integer; > ss,sp:set of 1..200; > min,t,j:integer; > ok:boolean; > begin > ss:=[1..kol];sp:=[]; > for i:=1 to kol do > if a[1,i]<>0 then > begin > p:=i; > break; > end; > w:=1; > ss:=ss-[1,p];sp:=[1,p]; > while ss<>[] do > begin > for i:=1 to kol do > begin > ok:=false; > if not(i in sp)then > for j:=1 to kol do > if (j in sp)and(a[i,j]<>0)then > begin > ss:=ss-[i,j]; > sp:=sp+[i,j]; > inc(w); > break; > ok:=true; > end; > if ok then break; > end; > end; > end; > procedure outt; > begin > writeln(w); > end; > begin > init; > solve; > outt; > end. Don't forget about min and max tests)) I submitted this problem many times and got WA2 See my code in C: int main() { gets(et); en=0; while (a[i-1][j-1]!='#') { j=0; while ((c=getc(stdin))!=10) { if (c=='-') { i++; j=0; continue; } if (c=='#') break; if (c==10||c==13) continue; a[i][j]=c; j++; //temp=getc(stdin); } if (c=='#') break; i++; if (a[i-1][j-1]=='#') { break;} //cout<<i<<endl; } //for (i=0;i<5;i++) // printf("%s",a[i]) ; // return 0; //}
en=k=i; for (i=0;i<k;i++) { if (e[i]==0) { e[i]=1; for (j=i+1;j<=k;j++) { if (strcmp(a[i],a[j])==0) { e[j]=1; en--; } } } } printf("%d\n",en-1); return 0; }
Thx very much. - Edited by author 19.08.2006 09:54 who can tell me Your program crashes on another line. Didn't you try to run your program on sample input? i tried on my pc, i managed to have it runned through i know that's all because im not familiar with the online java judge environment, but i just cannot get any feedback infomation from the email system, how does it come? here's my codes, i really dont know where and how import java.io.*; public class Application { public static void main(String[] args) throws IOException { new Application().run(); } StreamTokenizer in; PrintWriter out; static int n = 0; void run() throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader( System.in))); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.flush(); } void solve() throws IOException { String input = null; String[] strArray = new String[30]; while (true) { input = getString(); if (input.equals("#")) { break; } else { saveItToArray(strArray, input); } } checkOutIdentical(strArray); System.out.println(n - 1); } private static String getString() throws IOException { BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); String input; input = keyboard.readLine(); return (input); } void saveItToArray(String[] strArray, String input) { boolean isDash = false; for (int i = 0; i < input.length(); i++) { if (input.charAt(i) == '-') { strArray[n++] = input.substring(0, i); strArray[n++] = input.substring(i + 1, input.length()); isDash = true; break; } else { isDash = false; } } if (!isDash) { strArray[n++] = input; } } void checkOutIdentical(String[] strArray) { int count = 0; count = n; for (int i = 1; i < count; i++) { for (int j = i - 1; j >= 0; j--) { if (!strArray[i].equals(" ") && !strArray[j].equals(" ") && strArray[i].equals(strArray[j])) { strArray[i] = " "; n--; } } } } } getString() is wrong!!! BufferedReader(new InputStreamReader(System.in)) reads whole input during the first run of getString(), but you use only first line of it you may count number sluice 'N' and write 'n-1' The answer is (compartments - 1). |
|