首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

浙大 ACM 第1048题 找错!解决办法

2012-04-22 
浙大 ACM 第1048题 找错!!!这道题研究了两天,依旧无法AC,希望各位高手指点一二。小弟不胜感激。这是原题:ZOJ

浙大 ACM 第1048题 找错!!!
这道题研究了两天,依旧无法AC,希望各位高手指点一二。小弟不胜感激。

这是原题:

ZOJ Problem Set - 1084
Channel Allocation
Time Limit: 2 Seconds Memory Limit: 65536 KB
When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels.

Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.


INPUT

The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,...,I and J. A network with zero repeaters indicates the end of input.

Following the number of repeaters is a list of adjacency relationships. Each line has the form:

A:BCDH

which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form

A:

The repeaters are listed in alphabetical order.

Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross.


OUTPUT

For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.


SAMPLE INPUT

2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0


SAMPLE OUTPUT

1 channel needed.
3 channels needed.
4 channels needed. 



以下的是我的代码:

#include<iostream>
#include<string.h>
using namespace std;

struct tp
{
char h;
char s[26];
int m;
};
int main()
{
tp p[26];
int n,l,chan=1;
char str[27];
while(cin>>n&&n)
{

l=0;
for(int i=0;i<n;i++)
{
cin>>str;
l=strlen(str);
p[i].h=str[0];
p[i].m=l-2;
for(int j=0;j<l-2;j++)
{
int t;
for(t=2;t<n;t++)
{
p[i].s[j]=str[t];
}
p[i].s[t]='\0';
}
}

int flag,f,g,k,ct;
for(f=1;f<n;f++)
{
flag=0;
for(g=0;g<f;g++)
{
if(flag==1)
{
break;
}
ct=0;
for(k=0;k<p[g].m;k++)
{
if(p[f].h==p[g].s[k])
{
ct=1;
break;
}
}
if(k==p[g].m&&ct==0)
{
flag=1;
p[g].s[k+1]=p[f].h;
p[g].m++;
}
}
if(g==f&&flag==0)
{
chan++;
}
}
cout<<chan<<endl;
cout<<endl;
}
return 0;
}


一直找不出错误的地方,请各位指正。

[解决办法]


C/C++ code
 #include <iostream>    using namespace std;    int n, m;  bool map[26][26];    int x[26]; bool finded;    void search(int i)  {      if(finded)          return;      if(i > n)      {          finded = true;          return;      }      for(int col = 1; col <= m; col++)      {         x[i] = col;                    int k;          for(k = 0; k < n; k++)              if(map[i][k] && x[i] == x[k])                  break;          if(k == n)              search(i + 1);      }  }    int main()  {      freopen("p1084.in", "r", stdin);          while((cin >> n) && n)      {          memset(map, false, sizeof(map));                   char s, t; getchar();         for(int i = 0; i < n; i++)          {             scanf("%c:", &s);              while((t = getchar()) != '\n')                  map[s - 'A'][t - 'A'] = true;          }          for(m = 1; ; m++)          {              memset(x, 0, sizeof(x));             finded = false;             search(0);                            if(finded)              {                  cout << m << (m > 1 ? " channels " : " channel ") << "needed." << endl;                 break;              }          }      }            return 0;  } 

热点排行