#include<iostream>
using namespace std;
const int MaxLen=20;
void SetMatrix(int n,int Ma[MaxLen][MaxLen]) {
for (int i = 0; i< MaxLen; i++) {
for (int j = 0; j<MaxLen; j++) {
Ma[i][j] = 0;
}
}
for (int i = 0; i< n; i++) {
for (int j = 0; j<n; j++) {
cin >> Ma[i][j];
}
}
}
void DFS(int v,int n, int Ma[MaxLen][MaxLen],bool*visit) {
visit[v] = true;
cout << v << " ";
for (int i = 0; i<n; i++) {
if (Ma[v][i] == 1 && visit[i] != true) {
DFS(i,n,Ma,visit);
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int Ma[MaxLen][MaxLen];
SetMatrix(n, Ma);
bool*visit = new bool[n];
for (int i = 0; i < n; i++) {
visit[i] = false;
}
DFS(0, n, Ma, visit);
cout << endl;
}
}
答案错误的50%,好郁闷
因为没有加遍历函数,师兄说程序没有遍历到所有的点
#include<iostream>
#include<queue>
using namespace std;
const int MaxLen=20;
void SetMatrix(int n,int Ma[MaxLen][MaxLen]) {
for (int i = 0; i< MaxLen; i++) {
for (int j = 0; j<MaxLen; j++) {
Ma[i][j] = 0;
}
}
for (int i = 0; i< n; i++) {
for (int j = 0; j<n; j++) {
cin >> Ma[i][j];
}
}
}
void BFS(int m,int n, int Ma[MaxLen][MaxLen],bool*visit) {
queue<int>q;
q.push(m);
while (!q.empty()) {
int v = q.front();
if (visit[v] != true) {
cout << v << " ";
visit[v] = true;
}
q.pop();
for (int i = 0; i < n; i++) {
if (Ma[v][i] == 1 && visit[i] != true) {
visit[i] = true;
cout << i << " ";
q.push(i);
}
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int Ma[MaxLen][MaxLen];
SetMatrix(n, Ma);
bool*visit = new bool[n];
for (int i = 0; i < n; i++) {
visit[i] = false;
}
BFS(0, n, Ma, visit);
cout << endl;
}
return 0;
}
广度优先搜索正确
#include<iostream>
using namespace std;
struct line {
int info;
line*next;
};
struct head {
char data;
line*next;
};
void Create(int n,char*gr,head**vm,char p1,char p2) {
int f1,f2;
for (int i = 0; i < n; i++) {
if (p1 == gr[i]) {
f1 = i;
}
if (p2 == gr[i]) {
f2 = i;
}
}
line*p = vm[f1]->next;
while (p != NULL) {
p = p->next;
cout << " 1 2 3 ";//进不来,p = p->next;写成了p = vm[f1]->next->next;改了还是有问题
}
line*q = new line;
q->info = f2;
q->next = NULL;
p = q;
line*w = vm[f1]->next;
//检验
while (w != NULL) {
cout << w->info << endl;
w = w->next;
}//检验
cout << p->info << " ";
}
void Output(int n,head**vm) {
for (int i = 0; i < n; i++) {
cout << i << " ";
cout << vm[i]->data << "-";
line*p;
p = vm[i]->next;
while (p != NULL) {
cout << p->info << "-";
p = p->next;
}
cout << "^" << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
char*gr = new char[n];
for (int i = 0; i < n; i++) {
cin >> gr[i];
}
head**vmap = new head*[n];
for (int i = 0; i < n; i++) {
vmap[i] = new head;//和java一样,初始化不要有问题
vmap[i]->data = gr[i];
vmap[i]->next = NULL;
}
char p1, p2;
while (k--) {
cin >> p1 >> p2;
Create(n, gr, vmap, p1, p2);
}
Output(n, vmap);
}
return 0;
}
好奇怪,链表串不起来,
看来对链表的增删改查还是不熟练呀,尤其是对于地址的赋值
#include<iostream>
using namespace std;
struct line {
int info;
line*next;
};
struct head {
char data;
line*next;
};
void Create(int n,char*gr,head**vm,char p1,char p2) {
int f1,f2;
for (int i = 0; i < n; i++) {
if (p1 == gr[i]) {
f1 = i;
}
if (p2 == gr[i]) {
f2 = i;
}
}
line*p = new line;
//
if (vm[f1]->next == NULL) {
vm[f1]->next = p;
p->info = f2;
p->next = NULL;
}
else {
p = vm[f1]->next;
while (p->next != NULL) {
p = p->next;
}
line*q = new line;
q->next = p->next;
p->next = q;
q->info = f2;
}
}
void Output(int n,head**vm) {
for (int i = 0; i < n; i++) {
cout << i << " ";
cout << vm[i]->data << "-";
line*p;
p = vm[i]->next;
while (p != NULL) {
cout << p->info << "-";
p = p->next;
}
cout << "^" << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
char*gr = new char[n];
for (int i = 0; i < n; i++) {
cin >> gr[i];
}
head**vmap = new head*[n];
for (int i = 0; i < n; i++) {
vmap[i] = new head;//和java一样,初始化不要有问题
vmap[i]->data = gr[i];
vmap[i]->next = NULL;
}
char p1, p2;
while (k--) {
cin >> p1 >> p2;
Create(n, gr, vmap, p1, p2);
}
Output(n, vmap);
}
return 0;
}
自己根据老师那样改,反倒是不知道为什么而编译不了了,有点不爽,先记录下代码,有时间再看
#include<iostream>
using namespace std;
const int MaxLen = 20;
void SetMatrix(int n, int Ma[MaxLen][MaxLen]) {
for (int i = 0; i< MaxLen; i++) {
for (int j = 0; j<MaxLen; j++) {
Ma[i][j] = 0;
}
}
for (int i = 0; i< n; i++) {
for (int j = 0; j<n; j++) {
cin >> Ma[i][j];
}
}
}
void DFS(int v, int n, int Ma[MaxLen][MaxLen], bool*visit) {
int w,k;
visit[v] = true;
cout << v << " ";
int*Adjvex=new int[n];
for(int i=0;i<n;i++){
Adjvex[i]=-1;
}
k=0;
for (int i = 0; i<n; i++) {
if (Ma[v][i] == 1 ) {
Adjvex[k]=i;
k++;
}
}
w=0;
for (int i = 0; i<=k; i++) {
w=Adjvex[i];
while(w!=-1){
if(visit[w]!=true){
DFS(w, n, Ma, visit);
}
}
}
delete[] Adjvex;
}
void DFSTraverse(int v,int n,int Ma[MaxLen][MaxLen],bool*visit){
for (int i = 0; i < n; i++) {
visit[i] = false;
}
for(int i=v;i<n;i++){
if(visit[v]!=true){
DFS(v, n, Ma, visit);
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int Ma[MaxLen][MaxLen];
SetMatrix(n, Ma);
bool*visit = new bool[n];
DFSTraverse(0,n,Ma,visit);
cout << endl;
}
return 0;
}
前哥的代码:
#include <iostream>
using namespace std;
const int MaxLen = 20;
class Map
{
private:
bool Visit[MaxLen];
int Matrix[MaxLen][MaxLen];
int Vexnum;
void DFS(int v);
public:
void SetMatrix(int vnum,int mx[MaxLen][MaxLen]);
void DFSTraverse();
};
void Map::SetMatrix(int vnum,int mx[MaxLen][MaxLen])
{
int i,j;
Vexnum = vnum;
for ( i = 0; i < MaxLen; i++ )
{
for ( j = 0; j < MaxLen; j++)
Matrix[i][j] = 0;
}
for(i = 0;i<Vexnum;i++)
{
for(j = 0;j<Vexnum;j++)
Matrix[i][j] = mx[i][j];
}
}
void Map::DFSTraverse()
{
int v;
for(v = 0;v < Vexnum;v ++)
Visit[v] = false;
for(v = 0;v < Vexnum;v++)
{
if(!Visit[v])
DFS(v);
}
cout << endl;
}
void Map::DFS(int v)
{
int w,i,k;
Visit[v] = true;
cout << v << " ";
int *AdjVex = new int[Vexnum];
for(w = 0;w < Vexnum;w++)
AdjVex[w] = -1;
for(k = 0;k < Vexnum;k++)
{
if (Matrix[v][k] == 1)
{
AdjVex[k] = k;
}
}
i = 0;
for(i = 0;i < Vexnum;i++)
{
w = AdjVex[i];
if(w == -1)
continue;
if(!Visit[w])
DFS(w);
}
delete []AdjVex;
}
int main(int argc, char const *argv[])
{
Map map;
int t,i;
cin >> t;
for ( i = 0; i < t; i++)
{
int n,j,k;
cin >> n;
int mx[MaxLen][MaxLen];
for(j = 0;j < n;j++)
for(k = 0;k <n;k++)
cin >> mx[j][k];
map.SetMatrix(n,mx);
map.DFSTraverse();
}
return 0;
}
问题出在哪里?
#include<iostream>
using namespace std;
const int MaxLen = 20;
void SetMatrix(int n, int Ma[MaxLen][MaxLen]) {
for (int i = 0; i< MaxLen; i++) {
for (int j = 0; j<MaxLen; j++) {
Ma[i][j] = 0;
}
}
for (int i = 0; i< n; i++) {
for (int j = 0; j<n; j++) {
cin >> Ma[i][j];
}
}
}
void DFS(int v, int n, int Ma[MaxLen][MaxLen], bool*visit) {
int w;
visit[v] = true;
cout << v << " ";
int*Adjvex=new int[n];
for(int i=0;i<n;i++){
Adjvex[i]=-1;
}
for (int k=0; k<n; k++) {
if (Ma[v][k] == 1 ) {
Adjvex[k]=k;
}
}
for (int i = 0; i<=n; i++) {
w=Adjvex[i];
if(w == -1)
continue;
if(visit[w]!=true){
DFS(w, n, Ma, visit);
}
}
delete[] Adjvex;
}
void DFSTraverse(int v,int n,int Ma[MaxLen][MaxLen],bool*visit){
for (int i = 0; i < n; i++) {
visit[i] = false;
}
for(int v=0;v<n;v++){
if(visit[v]!=true){
DFS(v, n, Ma, visit);
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int Ma[MaxLen][MaxLen];
SetMatrix(n, Ma);
bool*visit = new bool[n];
DFSTraverse(0,n,Ma,visit);
cout << endl;
}
return 0;
}
第三题有点问题。错误50%,并且感觉自己写的有点累赘
#include<iostream>
#include<string>
using namespace std;
const int Max = 20;
int mar[Max][Max];
void dmap(int n) {
string*gr = new string[n];
for (int i = 0; i < n; i++) {
cin >> gr[i];
}
int len;
cin >> len;
for (int i = 0; i < Max; i++) {
for (int j = 0; j < Max; j++) {
mar[i][j] = -1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mar[i][j] = 0;
}
}
for (int i = 0; i < len; i++) {
string a1, a2;
cin >> a1;
cin >> a2;
int z = a1[1]-49;
int x = a2[1]-49;
mar[z][x] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n - 1) {
cout << mar[i][j] << endl;
}
else {
cout << mar[i][j] << " ";
}
}
}
for (int i = 0; i < n; i++) {
int h, j;
h = j = 0;
for (int e = 0; e < n; e++) {
if (mar[i][e] == 1)
h++;
}
for (int e = 0; e < n; e++) {
if (mar[e][i] == 1)
j++;
}
if (h + j == 0) {
cout << gr[i] << endl;
}
else {
cout << gr[i] << ": " << h << " " << j << " " << h + j << endl;
}
}
}
void umap(int n) {
char*gr = new char[n];
for (int i = 0; i < n; i++) {
cin >> gr[i];
}
int len;
cin >> len;
for (int i = 0; i < Max; i++) {
for (int j = 0; j < Max; j++) {
mar[i][j] = -1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mar[i][j] = 0;
}
}
for (int i = 0; i < len; i++) {
char a1, a2;
cin >> a1;
cin >> a2;
int k1, k2;
for (int j = 0; j < n; j++) {
if (a1 == gr[j])
k1 = j;
if (a2 == gr[j])
k2 = j;
}
mar[k1][k2] = 1;
mar[k2][k1] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n - 1) {
cout << mar[i][j] << endl;
}
else {
cout << mar[i][j] << " ";
}
}
}
for (int i = 0; i < n; i++) {
int k = 0;
for (int e = 0; e < n; e++) {
if (mar[i][e] == 1)
k++;
}
if (k == 0) {
cout << gr[i] << endl;
}
else {
cout << gr[i] << ": " << k << endl;
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
char c;
cin >> c;
if (c == 'D') {
int num;
cin >> num;
dmap(num);
}
if (c == 'U') {
int num;
cin >> num;
umap(num);
}
}
return 0;
}
感觉可能是二维矩阵的问题,题目没有说矩阵的大小限制
#include<iostream>
#include<string>
using namespace std;
//const int Max = 40;
//int mar[Max][Max];
void dmap(int n) {
string*gr = new string[n];
for (int i = 0; i < n; i++) {
cin >> gr[i];
}
int len;
cin >> len;
int**mar;
mar = new int*[n];
for (int i = 0; i < n; i++) {
mar[i] = new int[n];
for (int j = 0; j < n; j++) {
mar[i][j] = 0;
}
}
for (int i = 0; i < len; i++) {
string a1, a2;
cin >> a1;
cin >> a2;
int z = a1[1]-49;
int x = a2[1]-49;
mar[z][x] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n - 1) {
cout << mar[i][j] << endl;
}
else {
cout << mar[i][j] << " ";
}
}
}
for (int i = 0; i < n; i++) {
int h, j;
h = j = 0;
for (int e = 0; e < n; e++) {
if (mar[i][e] == 1)
h++;
}
for (int e = 0; e < n; e++) {
if (mar[e][i] == 1)
j++;
}
if (h + j == 0) {
cout << gr[i] << endl;
}
else {
cout << gr[i] << ": " << h << " " << j << " " << h + j << endl;
}
}
}
void umap(int n) {
char*gr = new char[n];
for (int i = 0; i < n; i++) {
cin >> gr[i];
}
int len;
cin >> len;
int**mar;
mar = new int*[n];
for (int i = 0; i < n; i++) {
mar[i] = new int[n];
for (int j = 0; j < n; j++) {
mar[i][j] = 0;
}
}
for (int i = 0; i < len; i++) {
char a1, a2;
cin >> a1;
cin >> a2;
int k1, k2;
for (int j = 0; j < n; j++) {
if (a1 == gr[j])
k1 = j;
if (a2 == gr[j])
k2 = j;
}
mar[k1][k2] = 1;
mar[k2][k1] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n - 1) {
cout << mar[i][j] << endl;
}
else {
cout << mar[i][j] << " ";
}
}
}
for (int i = 0; i < n; i++) {
int k = 0;
for (int e = 0; e < n; e++) {
if (mar[i][e] == 1)
k++;
}
if (k == 0) {
cout << gr[i] << endl;
}
else {
cout << gr[i] << ": " << k << endl;
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
char c;
cin >> c;
if (c == 'D') {
int num;
cin >> num;
dmap(num);
}
if (c == 'U') {
int num;
cin >> num;
umap(num);
}
}
return 0;
}
这个时候OJ显示指针访问出界,有点问题哦。
搞定,果然是数据没有考虑周全,或者是以为自己考虑周全了其实没有。在刚开始写下每一行代码的时候就应该注意各种情况,甚至写之前就想清楚,而不是在写完过不了的时候再去思考有什么没有考虑到。
#include<iostream>
#include<string>
using namespace std;
const int Max = 20;
int mar[Max][Max];
void dmap(int n) {
string*gr = new string[n];
for (int i = 0; i < n; i++) {
cin >> gr[i];
}
int len;
cin >> len;
for (int i = 0; i < Max; i++) {
for (int j = 0; j < Max; j++) {
mar[i][j] = -1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mar[i][j] = 0;
}
}
for (int i = 0; i < len; i++) {
string a1, a2;
cin >> a1;
cin >> a2;
int z, x;
for (int i = 0; i < n; i++) {
if (gr[i] == a1) {
z = i;
}
if (gr[i] == a2) {
x = i;
}
}
mar[z][x] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n - 1) {
cout << mar[i][j] << endl;
}
else {
cout << mar[i][j] << " ";
}
}
}
for (int i = 0; i < n; i++) {
int h, j;
h = j = 0;
for (int e = 0; e < n; e++) {
if (mar[i][e] == 1)
h++;
}
for (int e = 0; e < n; e++) {
if (mar[e][i] == 1)
j++;
}
if (h + j == 0) {
cout << gr[i] << endl;
}
else {
cout << gr[i] << ": " << h << " " << j << " " << h + j << endl;
}
}
}
void umap(int n) {
char*gr = new char[n];
for (int i = 0; i < n; i++) {
cin >> gr[i];
}
int len;
cin >> len;
for (int i = 0; i < Max; i++) {
for (int j = 0; j < Max; j++) {
mar[i][j] = -1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mar[i][j] = 0;
}
}
for (int i = 0; i < len; i++) {
char a1, a2;
cin >> a1;
cin >> a2;
int k1, k2;
for (int j = 0; j < n; j++) {
if (a1 == gr[j])
k1 = j;
if (a2 == gr[j])
k2 = j;
}
mar[k1][k2] = 1;
mar[k2][k1] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n - 1) {
cout << mar[i][j] << endl;
}
else {
cout << mar[i][j] << " ";
}
}
}
for (int i = 0; i < n; i++) {
int k = 0;
for (int e = 0; e < n; e++) {
if (mar[i][e] == 1)
k++;
}
if (k == 0) {
cout << gr[i] << endl;
}
else {
cout << gr[i] << ": " << k << endl;
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
char c;
cin >> c;
if (c == 'D') {
int num;
cin >> num;
dmap(num);
}
if (c == 'U') {
int num;
cin >> num;
umap(num);
}
}
return 0;
}