首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >(dart)place函数保存St初始状态的Keepst里的内容会随St改变比如n1k1i0j3时?

(dart)place函数保存St初始状态的Keepst里的内容会随St改变比如n1k1i0j3时?

提问于 2023-10-22 14:51:46
回答 0关注 0查看 111

完成老师作业,编写代码遇到了以下问题,怎么都解决不了,函数place中用的Keepst保存St的初始状态,不希望Keepst改变,可每次当n=1,k=1,i=0,j=3时,st发生改变后,Keepst也会跟着改变,在n=1,k=1,i=0,j=1时st改变,Keepst不会变,以下是代码,

代码语言:javascript
复制
//place,里的保存初始状态的Keepst,不知道为什么里面的内容总是会随着St改变,怎么都不行
class Solution {
  int guardCastle(List<String> grid) {
    List<String> st = grid[0].split(''),
        nd = grid[1].split(''),
        keepst = List.from(st),
        keepnd = List.from(nd); //初始化
    int answer = -2;
    grid=move(List.from(st), List.from(nd));//函数传参不能传参数本身,只传值List<String>.from()
    st=grid[0].split('');
    nd=grid[1].split('');//无放置,更新数据
    if (st.contains('C') || nd.contains('C'))
      return 0;
    else {
      st = keepst;
      nd = keepnd;
    }
    for (int i = 1; i < grid[0].length; i++) {
      //放置
      answer = place(List.from(st),List.from(nd), i, 1);
      if (answer > 0 || i + 1 == grid[0].length) return answer;
    }
    return answer;
  }

  List<String> move(List<String> St, List<String> Nd) {
    for (int i = 0; i < St.length; i++) {
      //往下移动
      if (St[i] == 'S' && Nd[i] != '#' && Nd[i] != 'S') {
        if (Nd[i] == 'P') {
          for (int j = 0; j < St.length; j++) {
            if (St[j] == 'P') St[j] = 'S';
            if (Nd[j] == 'P') Nd[j] = 'S';
          }
        }
        Nd[i] = 'S';
        i = -1;
        //往上移动
      } else if (Nd[i] == 'S' && St[i] != '#' && St[i] != 'S') {
        if (St[i] == 'P') {
          for (int j = 0; j < St.length; j++) {
            if (St[j] == 'P') St[j] = 'S';
            if (Nd[j] == 'P') Nd[j] = 'S';
          }
        }
        St[i] = 'S';
        i = -1;
        //第一行往右移动
      } else if (i < St.length - 1 &&
          St[i] == 'S' &&
          St[i + 1] != '#' &&
          St[i + 1] != 'S') {
        if (St[i + 1] == 'P') {
          for (int j = 0; j < St.length; j++) {
            if (St[j] == 'P') St[j] = 'S';
            if (Nd[j] == 'P') Nd[j] = 'S';
          }
        }
        St[i + 1] = 'S';
        i = -1;
        //第一行往左移动
      } else if (i > 0 &&
          St[i] == 'S' &&
          St[i - 1] != '#' &&
          St[i - 1] != 'S') {
        if (St[i - 1] == 'P') {
          for (int j = 0; j < St.length; j++) {
            if (St[j] == 'P') St[j] = 'S';
            if (Nd[j] == 'P') Nd[j] = 'S';
          }
        }
        St[i - 1] = 'S';
        i = -1;
        //第二行往右移动
      } else if (i < Nd.length - 1 &&
          Nd[i] == 'S' &&
          Nd[i + 1] != '#' &&
          Nd[i + 1] != 'S') {
        if (Nd[i + 1] == 'P') {
          for (int j = 0; j < St.length; j++) {
            if (St[j] == 'P') St[j] = 'S';
            if (Nd[j] == 'P') Nd[j] = 'S';
          }
        }
        Nd[i + 1] = 'S';
        i = -1;
        //第二行往左移动
      } else if (i > 0 &&
          Nd[i] == 'S' &&
          Nd[i - 1] != '#' &&
          Nd[i - 1] != 'S') {
        if (Nd[i - 1] == 'P') {
          for (int j = 0; j < St.length; j++) {
            if (St[j] == 'P') St[j] = 'S';
            if (Nd[j] == 'P') Nd[j] = 'S';
          }
        }
        Nd[i - 1] = 'S';
        i = -1;
      }
    }
    List<String> Grid=[St.join(),Nd.join()];
    return Grid;
  }

  int place(List<String> St,List<String> Nd, int n, int k) {
    List<String> Keepst = List.from(St), Keepnd = List.from(Nd); //初始化
    List<String> Grid = [St.join(),Nd.join()];
    int Answer = -3;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < Grid[0].length; j++) {
        if (i == 0 && Grid[i][j] == '.') {
          //第一行
          St[j] = '#';
          if (k < n) {
            Answer = place(List.from(St),List.from(Nd), n, k++);
          }
          if (Answer > 0) return Answer;
          Grid=move(List.from(St), List.from(Nd));
          St=Grid[0].split('');
          Nd=Grid[1].split('');
          if (St.contains('C') || Nd.contains('C'))
            return k;
          else {
            St = Keepst;
            Nd = Keepnd;
            Grid = [St.join(),Nd.join()];
          }
        } else if (i == 1 && Grid[i][j] == '.') {
          //第二行
          Nd[j] = '#';
          if (k < n) {
            Answer = place(List.from(St),List.from(Nd), n, k++);
          }
          if (Answer > 0) return Answer;
          Grid=move(List.from(St), List.from(Nd));
          St=Grid[0].split('');
          Nd=Grid[1].split('');
          if (St.contains('C') || Nd.contains('C'))
            return k;
          else {
            St = Keepst;
            Nd = Keepnd;
            Grid = [St.join(),Nd.join()];
          }
        }
        if (i == 1 && j + 1 == Grid[0].length) return -1;
      }
    }
    return -3;
  }
}

void main() {
  List<String> grid = ["S.C.P#P.", ".....#.S"];
  Solution solution = Solution();
  int result = solution.guardCastle(grid);
  print(result);
}

在断点,place函数中

代码语言:javascript
复制
          St[j] = '#';
          if (k < n) {

时,调试发现问题,求助各位大佬

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档